********************************************************************************
** 	TITLE:		ca2015_lpp				                                      **
**  AUTHORS:    P. Mongrain, N. Fréchet, B. Thompson Collart, and Y. Dufresne **
**	DATA:       LPP2015					                                      **
**	DATE:		February 2025	 					                          **	
**  VERSION:	Stata 17					                                  **	
********************************************************************************

* Note: The data file needs to be downloaded from its original source (see the "surveys.text" file)

* Version control

version 16.0

* Open log file

capture log close
log using "ca2015_lpp", replace

* Open dataset

use "LPP2015.dta", clear

* Election

gen election = "ca2015"

* Type of survey

gen type = "lpp"

* Type of expectation item

gen probability = 1

* Respondent ID

encode rnid, gen(responseid)

* Survey weight

merge 1:1 election responseid using ca2015_lpp_weight
drop _merge


***********************
** DISTRICT OUTCOMES **
***********************

* Election outcome in each district (1 = CPC, 2 = LPC, 3 = NDP, 4 = BQ, 5 = GPC, 88 = other)

gen district_outcome = .

replace district_outcome = 2 if constituencynumber == 10001
replace district_outcome = 2 if constituencynumber == 10002
replace district_outcome = 2 if constituencynumber == 10003
replace district_outcome = 2 if constituencynumber == 10004
replace district_outcome = 2 if constituencynumber == 10005
replace district_outcome = 2 if constituencynumber == 10006
replace district_outcome = 2 if constituencynumber == 10007
replace district_outcome = 2 if constituencynumber == 11001
replace district_outcome = 2 if constituencynumber == 11002
replace district_outcome = 2 if constituencynumber == 11003
replace district_outcome = 2 if constituencynumber == 11004
replace district_outcome = 2 if constituencynumber == 12001
replace district_outcome = 2 if constituencynumber == 12002
replace district_outcome = 2 if constituencynumber == 12003
replace district_outcome = 2 if constituencynumber == 12004
replace district_outcome = 2 if constituencynumber == 12005
replace district_outcome = 2 if constituencynumber == 12006
replace district_outcome = 2 if constituencynumber == 12007
replace district_outcome = 2 if constituencynumber == 12008
replace district_outcome = 2 if constituencynumber == 12009
replace district_outcome = 2 if constituencynumber == 12010
replace district_outcome = 2 if constituencynumber == 12011
replace district_outcome = 2 if constituencynumber == 13001
replace district_outcome = 2 if constituencynumber == 13002
replace district_outcome = 2 if constituencynumber == 13003
replace district_outcome = 2 if constituencynumber == 13004
replace district_outcome = 2 if constituencynumber == 13005
replace district_outcome = 2 if constituencynumber == 13006
replace district_outcome = 2 if constituencynumber == 13007
replace district_outcome = 2 if constituencynumber == 13008
replace district_outcome = 2 if constituencynumber == 13009
replace district_outcome = 2 if constituencynumber == 13010
replace district_outcome = 3 if constituencynumber == 24001
replace district_outcome = 3 if constituencynumber == 24002
replace district_outcome = 2 if constituencynumber == 24003
replace district_outcome = 2 if constituencynumber == 24004
replace district_outcome = 2 if constituencynumber == 24005
replace district_outcome = 2 if constituencynumber == 24006
replace district_outcome = 1 if constituencynumber == 24007
replace district_outcome = 1 if constituencynumber == 24008
replace district_outcome = 4 if constituencynumber == 24009
replace district_outcome = 1 if constituencynumber == 24010
replace district_outcome = 3 if constituencynumber == 24011
replace district_outcome = 3 if constituencynumber == 24012
replace district_outcome = 2 if constituencynumber == 24013
replace district_outcome = 4 if constituencynumber == 24014
replace district_outcome = 2 if constituencynumber == 24015
replace district_outcome = 2 if constituencynumber == 24016
replace district_outcome = 2 if constituencynumber == 24017
replace district_outcome = 3 if constituencynumber == 24018
replace district_outcome = 1 if constituencynumber == 24019
replace district_outcome = 1 if constituencynumber == 24020
replace district_outcome = 2 if constituencynumber == 24021
replace district_outcome = 2 if constituencynumber == 24022
replace district_outcome = 2 if constituencynumber == 24023
replace district_outcome = 2 if constituencynumber == 24024
replace district_outcome = 3 if constituencynumber == 24025
replace district_outcome = 2 if constituencynumber == 24026
replace district_outcome = 2 if constituencynumber == 24027
replace district_outcome = 3 if constituencynumber == 24028
replace district_outcome = 2 if constituencynumber == 24029
replace district_outcome = 2 if constituencynumber == 24030
replace district_outcome = 4 if constituencynumber == 24031
replace district_outcome = 3 if constituencynumber == 24032
replace district_outcome = 4 if constituencynumber == 24033
replace district_outcome = 2 if constituencynumber == 24034
replace district_outcome = 1 if constituencynumber == 24035
replace district_outcome = 2 if constituencynumber == 24036
replace district_outcome = 2 if constituencynumber == 24037
replace district_outcome = 2 if constituencynumber == 24038
replace district_outcome = 3 if constituencynumber == 24039
replace district_outcome = 2 if constituencynumber == 24040
replace district_outcome = 2 if constituencynumber == 24041
replace district_outcome = 1 if constituencynumber == 24042
replace district_outcome = 3 if constituencynumber == 24043
replace district_outcome = 2 if constituencynumber == 24044
replace district_outcome = 1 if constituencynumber == 24045
replace district_outcome = 4 if constituencynumber == 24046
replace district_outcome = 1 if constituencynumber == 24047
replace district_outcome = 4 if constituencynumber == 24048
replace district_outcome = 2 if constituencynumber == 24049
replace district_outcome = 4 if constituencynumber == 24050
replace district_outcome = 1 if constituencynumber == 24051
replace district_outcome = 2 if constituencynumber == 24052
replace district_outcome = 2 if constituencynumber == 24053
replace district_outcome = 3 if constituencynumber == 24054
replace district_outcome = 2 if constituencynumber == 24055
replace district_outcome = 2 if constituencynumber == 24056
replace district_outcome = 2 if constituencynumber == 24057
replace district_outcome = 1 if constituencynumber == 24058
replace district_outcome = 2 if constituencynumber == 24059
replace district_outcome = 4 if constituencynumber == 24060
replace district_outcome = 1 if constituencynumber == 24061
replace district_outcome = 2 if constituencynumber == 24062
replace district_outcome = 4 if constituencynumber == 24063
replace district_outcome = 3 if constituencynumber == 24064
replace district_outcome = 2 if constituencynumber == 24065
replace district_outcome = 3 if constituencynumber == 24066
replace district_outcome = 2 if constituencynumber == 24067
replace district_outcome = 2 if constituencynumber == 24068
replace district_outcome = 2 if constituencynumber == 24069
replace district_outcome = 2 if constituencynumber == 24070
replace district_outcome = 3 if constituencynumber == 24071
replace district_outcome = 2 if constituencynumber == 24072
replace district_outcome = 3 if constituencynumber == 24073
replace district_outcome = 2 if constituencynumber == 24074
replace district_outcome = 4 if constituencynumber == 24075
replace district_outcome = 3 if constituencynumber == 24076
replace district_outcome = 2 if constituencynumber == 24077
replace district_outcome = 2 if constituencynumber == 24078
replace district_outcome = 2 if constituencynumber == 35001
replace district_outcome = 3 if constituencynumber == 35002
replace district_outcome = 2 if constituencynumber == 35003
replace district_outcome = 1 if constituencynumber == 35004
replace district_outcome = 1 if constituencynumber == 35005
replace district_outcome = 2 if constituencynumber == 35006
replace district_outcome = 2 if constituencynumber == 35007
replace district_outcome = 2 if constituencynumber == 35008
replace district_outcome = 2 if constituencynumber == 35009
replace district_outcome = 2 if constituencynumber == 35010
replace district_outcome = 2 if constituencynumber == 35011
replace district_outcome = 2 if constituencynumber == 35012
replace district_outcome = 1 if constituencynumber == 35013
replace district_outcome = 1 if constituencynumber == 35014
replace district_outcome = 2 if constituencynumber == 35015
replace district_outcome = 2 if constituencynumber == 35016
replace district_outcome = 1 if constituencynumber == 35017
replace district_outcome = 2 if constituencynumber == 35018
replace district_outcome = 2 if constituencynumber == 35019
replace district_outcome = 2 if constituencynumber == 35020
replace district_outcome = 2 if constituencynumber == 35021
replace district_outcome = 1 if constituencynumber == 35022
replace district_outcome = 1 if constituencynumber == 35023
replace district_outcome = 2 if constituencynumber == 35024
replace district_outcome = 1 if constituencynumber == 35025
replace district_outcome = 3 if constituencynumber == 35026
replace district_outcome = 2 if constituencynumber == 35027
replace district_outcome = 2 if constituencynumber == 35028
replace district_outcome = 2 if constituencynumber == 35029
replace district_outcome = 1 if constituencynumber == 35030
replace district_outcome = 2 if constituencynumber == 35031
replace district_outcome = 2 if constituencynumber == 35032
replace district_outcome = 1 if constituencynumber == 35033
replace district_outcome = 1 if constituencynumber == 35034
replace district_outcome = 3 if constituencynumber == 35035
replace district_outcome = 2 if constituencynumber == 35036
replace district_outcome = 3 if constituencynumber == 35037
replace district_outcome = 2 if constituencynumber == 35038
replace district_outcome = 2 if constituencynumber == 35039
replace district_outcome = 1 if constituencynumber == 35040
replace district_outcome = 2 if constituencynumber == 35041
replace district_outcome = 2 if constituencynumber == 35042
replace district_outcome = 2 if constituencynumber == 35043
replace district_outcome = 2 if constituencynumber == 35044
replace district_outcome = 2 if constituencynumber == 35045
replace district_outcome = 1 if constituencynumber == 35046
replace district_outcome = 2 if constituencynumber == 35047
replace district_outcome = 1 if constituencynumber == 35048
replace district_outcome = 1 if constituencynumber == 35049
replace district_outcome = 1 if constituencynumber == 35050
replace district_outcome = 3 if constituencynumber == 35051
replace district_outcome = 2 if constituencynumber == 35052
replace district_outcome = 2 if constituencynumber == 35053
replace district_outcome = 2 if constituencynumber == 35054
replace district_outcome = 2 if constituencynumber == 35055
replace district_outcome = 1 if constituencynumber == 35056
replace district_outcome = 1 if constituencynumber == 35057
replace district_outcome = 2 if constituencynumber == 35058
replace district_outcome = 2 if constituencynumber == 35059
replace district_outcome = 2 if constituencynumber == 35060
replace district_outcome = 2 if constituencynumber == 35061
replace district_outcome = 2 if constituencynumber == 35062
replace district_outcome = 2 if constituencynumber == 35063
replace district_outcome = 2 if constituencynumber == 35064
replace district_outcome = 2 if constituencynumber == 35065
replace district_outcome = 2 if constituencynumber == 35066
replace district_outcome = 1 if constituencynumber == 35067
replace district_outcome = 1 if constituencynumber == 35068
replace district_outcome = 2 if constituencynumber == 35069
replace district_outcome = 2 if constituencynumber == 35070
replace district_outcome = 2 if constituencynumber == 35071
replace district_outcome = 2 if constituencynumber == 35072
replace district_outcome = 2 if constituencynumber == 35073
replace district_outcome = 1 if constituencynumber == 35074
replace district_outcome = 2 if constituencynumber == 35075
replace district_outcome = 2 if constituencynumber == 35076
replace district_outcome = 2 if constituencynumber == 35077
replace district_outcome = 2 if constituencynumber == 35078
replace district_outcome = 2 if constituencynumber == 35079
replace district_outcome = 1 if constituencynumber == 35080
replace district_outcome = 2 if constituencynumber == 35081
replace district_outcome = 1 if constituencynumber == 35082
replace district_outcome = 1 if constituencynumber == 35083
replace district_outcome = 2 if constituencynumber == 35084
replace district_outcome = 2 if constituencynumber == 35085
replace district_outcome = 1 if constituencynumber == 35086
replace district_outcome = 2 if constituencynumber == 35087
replace district_outcome = 1 if constituencynumber == 35088
replace district_outcome = 2 if constituencynumber == 35089
replace district_outcome = 2 if constituencynumber == 35090
replace district_outcome = 1 if constituencynumber == 35091
replace district_outcome = 2 if constituencynumber == 35092
replace district_outcome = 2 if constituencynumber == 35093
replace district_outcome = 2 if constituencynumber == 35094
replace district_outcome = 2 if constituencynumber == 35095
replace district_outcome = 2 if constituencynumber == 35096
replace district_outcome = 2 if constituencynumber == 35097
replace district_outcome = 2 if constituencynumber == 35098
replace district_outcome = 1 if constituencynumber == 35099
replace district_outcome = 1 if constituencynumber == 35100
replace district_outcome = 2 if constituencynumber == 35101
replace district_outcome = 1 if constituencynumber == 35102
replace district_outcome = 2 if constituencynumber == 35103
replace district_outcome = 1 if constituencynumber == 35104
replace district_outcome = 2 if constituencynumber == 35105
replace district_outcome = 2 if constituencynumber == 35106
replace district_outcome = 3 if constituencynumber == 35107
replace district_outcome = 2 if constituencynumber == 35108
replace district_outcome = 2 if constituencynumber == 35109
replace district_outcome = 2 if constituencynumber == 35110
replace district_outcome = 2 if constituencynumber == 35111
replace district_outcome = 2 if constituencynumber == 35112
replace district_outcome = 1 if constituencynumber == 35113
replace district_outcome = 2 if constituencynumber == 35114
replace district_outcome = 2 if constituencynumber == 35115
replace district_outcome = 3 if constituencynumber == 35116
replace district_outcome = 3 if constituencynumber == 35117
replace district_outcome = 2 if constituencynumber == 35118
replace district_outcome = 1 if constituencynumber == 35119
replace district_outcome = 2 if constituencynumber == 35120
replace district_outcome = 2 if constituencynumber == 35121
replace district_outcome = 1 if constituencynumber == 46001
replace district_outcome = 2 if constituencynumber == 46002
replace district_outcome = 3 if constituencynumber == 46003
replace district_outcome = 1 if constituencynumber == 46004
replace district_outcome = 3 if constituencynumber == 46005
replace district_outcome = 2 if constituencynumber == 46006
replace district_outcome = 1 if constituencynumber == 46007
replace district_outcome = 1 if constituencynumber == 46008
replace district_outcome = 2 if constituencynumber == 46009
replace district_outcome = 1 if constituencynumber == 46010
replace district_outcome = 2 if constituencynumber == 46011
replace district_outcome = 2 if constituencynumber == 46012
replace district_outcome = 2 if constituencynumber == 46013
replace district_outcome = 2 if constituencynumber == 46014
replace district_outcome = 1 if constituencynumber == 47001
replace district_outcome = 1 if constituencynumber == 47002
replace district_outcome = 3 if constituencynumber == 47003
replace district_outcome = 1 if constituencynumber == 47004
replace district_outcome = 1 if constituencynumber == 47005
replace district_outcome = 1 if constituencynumber == 47006
replace district_outcome = 3 if constituencynumber == 47007
replace district_outcome = 1 if constituencynumber == 47008
replace district_outcome = 2 if constituencynumber == 47009
replace district_outcome = 1 if constituencynumber == 47010
replace district_outcome = 1 if constituencynumber == 47011
replace district_outcome = 3 if constituencynumber == 47012
replace district_outcome = 1 if constituencynumber == 47013
replace district_outcome = 1 if constituencynumber == 47014
replace district_outcome = 1 if constituencynumber == 48001
replace district_outcome = 1 if constituencynumber == 48002
replace district_outcome = 1 if constituencynumber == 48003
replace district_outcome = 2 if constituencynumber == 48004
replace district_outcome = 1 if constituencynumber == 48005
replace district_outcome = 1 if constituencynumber == 48006
replace district_outcome = 1 if constituencynumber == 48007
replace district_outcome = 1 if constituencynumber == 48008
replace district_outcome = 1 if constituencynumber == 48009
replace district_outcome = 1 if constituencynumber == 48010
replace district_outcome = 1 if constituencynumber == 48011
replace district_outcome = 1 if constituencynumber == 48012
replace district_outcome = 2 if constituencynumber == 48013
replace district_outcome = 2 if constituencynumber == 48014
replace district_outcome = 1 if constituencynumber == 48015
replace district_outcome = 1 if constituencynumber == 48016
replace district_outcome = 2 if constituencynumber == 48017
replace district_outcome = 1 if constituencynumber == 48018
replace district_outcome = 3 if constituencynumber == 48019
replace district_outcome = 1 if constituencynumber == 48020
replace district_outcome = 1 if constituencynumber == 48021
replace district_outcome = 1 if constituencynumber == 48022
replace district_outcome = 1 if constituencynumber == 48023
replace district_outcome = 1 if constituencynumber == 48024
replace district_outcome = 1 if constituencynumber == 48025
replace district_outcome = 1 if constituencynumber == 48026
replace district_outcome = 1 if constituencynumber == 48027
replace district_outcome = 1 if constituencynumber == 48028
replace district_outcome = 1 if constituencynumber == 48029
replace district_outcome = 1 if constituencynumber == 48030
replace district_outcome = 1 if constituencynumber == 48031
replace district_outcome = 1 if constituencynumber == 48032
replace district_outcome = 1 if constituencynumber == 48033
replace district_outcome = 1 if constituencynumber == 48034
replace district_outcome = 1 if constituencynumber == 59001
replace district_outcome = 2 if constituencynumber == 59002
replace district_outcome = 3 if constituencynumber == 59003
replace district_outcome = 1 if constituencynumber == 59004
replace district_outcome = 1 if constituencynumber == 59005
replace district_outcome = 1 if constituencynumber == 59006
replace district_outcome = 2 if constituencynumber == 59007
replace district_outcome = 2 if constituencynumber == 59008
replace district_outcome = 3 if constituencynumber == 59009
replace district_outcome = 3 if constituencynumber == 59010
replace district_outcome = 2 if constituencynumber == 59011
replace district_outcome = 2 if constituencynumber == 59012
replace district_outcome = 1 if constituencynumber == 59013
replace district_outcome = 2 if constituencynumber == 59014
replace district_outcome = 3 if constituencynumber == 59015
replace district_outcome = 1 if constituencynumber == 59016
replace district_outcome = 2 if constituencynumber == 59017
replace district_outcome = 3 if constituencynumber == 59018
replace district_outcome = 3 if constituencynumber == 59019
replace district_outcome = 1 if constituencynumber == 59020
replace district_outcome = 2 if constituencynumber == 59021
replace district_outcome = 2 if constituencynumber == 59022
replace district_outcome = 3 if constituencynumber == 59023
replace district_outcome = 1 if constituencynumber == 59024
replace district_outcome = 1 if constituencynumber == 59025
replace district_outcome = 3 if constituencynumber == 59026
replace district_outcome = 5 if constituencynumber == 59027
replace district_outcome = 3 if constituencynumber == 59028
replace district_outcome = 3 if constituencynumber == 59029
replace district_outcome = 1 if constituencynumber == 59030
replace district_outcome = 2 if constituencynumber == 59031
replace district_outcome = 2 if constituencynumber == 59032
replace district_outcome = 2 if constituencynumber == 59033
replace district_outcome = 2 if constituencynumber == 59034
replace district_outcome = 3 if constituencynumber == 59035
replace district_outcome = 2 if constituencynumber == 59036
replace district_outcome = 3 if constituencynumber == 59037
replace district_outcome = 3 if constituencynumber == 59038
replace district_outcome = 2 if constituencynumber == 59039
replace district_outcome = 2 if constituencynumber == 59040
replace district_outcome = 3 if constituencynumber == 59041
replace district_outcome = 2 if constituencynumber == 59042
replace district_outcome = 2 if constituencynumber == 60001
replace district_outcome = 2 if constituencynumber == 61001
replace district_outcome = 2 if constituencynumber == 62001


*********************
** COMPETITIVENESS **
*********************

* Margin of victory by district

gen margin = .

replace margin = 38.1 if constituencynumber == 10001
replace margin = 71.7 if constituencynumber == 10002
replace margin = 56.5 if constituencynumber == 10003
replace margin = 57.4 if constituencynumber == 10004
replace margin = 61.7 if constituencynumber == 10005
replace margin = 1.4 if constituencynumber == 10006
replace margin = 21.1 if constituencynumber == 10007
replace margin = 48.9 if constituencynumber == 11001
replace margin = 33.1 if constituencynumber == 11002
replace margin = 20.3 if constituencynumber == 11003
replace margin = 44.5 if constituencynumber == 11004
replace margin = 59.9 if constituencynumber == 12001
replace margin = 32.7 if constituencynumber == 12002
replace margin = 37.3 if constituencynumber == 12003
replace margin = 33.8 if constituencynumber == 12004
replace margin = 15.6 if constituencynumber == 12005
replace margin = 53 if constituencynumber == 12006
replace margin = 52.2 if constituencynumber == 12007
replace margin = 13.6 if constituencynumber == 12008
replace margin = 34.4 if constituencynumber == 12009
replace margin = 60.1 if constituencynumber == 12010
replace margin = 36.9 if constituencynumber == 12011
replace margin = 11.3 if constituencynumber == 13001
replace margin = 53.9 if constituencynumber == 13002
replace margin = 20.8 if constituencynumber == 13003
replace margin = 3.8 if constituencynumber == 13004
replace margin = 29.8 if constituencynumber == 13005
replace margin = 13 if constituencynumber == 13006
replace margin = 36.3 if constituencynumber == 13007
replace margin = 5.4 if constituencynumber == 13008
replace margin = 18.3 if constituencynumber == 13009
replace margin = 9.6 if constituencynumber == 13010
replace margin = 4.9 if constituencynumber == 24001
replace margin = 11.9 if constituencynumber == 24002
replace margin = 16.8 if constituencynumber == 24003
replace margin = 20.5 if constituencynumber == 24004
replace margin = 18.5 if constituencynumber == 24005
replace margin = 18.5 if constituencynumber == 24006
replace margin = 36.6 if constituencynumber == 24007
replace margin = 5.1 if constituencynumber == 24008
replace margin = 15.7 if constituencynumber == 24009
replace margin = 30.2 if constituencynumber == 24010
replace margin = 1.7 if constituencynumber == 24011
replace margin = 16.4 if constituencynumber == 24012
replace margin = 5.4 if constituencynumber == 24013
replace margin = 0.4 if constituencynumber == 24014
replace margin = 36.9 if constituencynumber == 24015
replace margin = 19.4 if constituencynumber == 24016
replace margin = 25.7 if constituencynumber == 24017
replace margin = 15.1 if constituencynumber == 24018
replace margin = 19 if constituencynumber == 24019
replace margin = 6.6 if constituencynumber == 24020
replace margin = 14.7 if constituencynumber == 24021
replace margin = 1.4 if constituencynumber == 24022
replace margin = 9.5 if constituencynumber == 24023
replace margin = 33.3 if constituencynumber == 24024
replace margin = 3.9 if constituencynumber == 24025
replace margin = 6.2 if constituencynumber == 24026
replace margin = 27.2 if constituencynumber == 24027
replace margin = 1 if constituencynumber == 24028
replace margin = 40.1 if constituencynumber == 24029
replace margin = 19.9 if constituencynumber == 24030
replace margin = 5.1 if constituencynumber == 24031
replace margin = 0.7 if constituencynumber == 24032
replace margin = 5 if constituencynumber == 24033
replace margin = 10.2 if constituencynumber == 24034
replace margin = 4.8 if constituencynumber == 24035
replace margin = 46.7 if constituencynumber == 24036
replace margin = 14.9 if constituencynumber == 24037
replace margin = 2.4 if constituencynumber == 24038
replace margin = 9.6 if constituencynumber == 24039
replace margin = 27.9 if constituencynumber == 24040
replace margin = 8.4 if constituencynumber == 24041
replace margin = 28.4 if constituencynumber == 24042
replace margin = 1.2 if constituencynumber == 24043
replace margin = 7.7 if constituencynumber == 24044
replace margin = 29 if constituencynumber == 24045
replace margin = 11.9 if constituencynumber == 24046
replace margin = 7.3 if constituencynumber == 24047
replace margin = 1.4 if constituencynumber == 24048
replace margin = 4.1 if constituencynumber == 24049
replace margin = 9.3 if constituencynumber == 24050
replace margin = 0.6 if constituencynumber == 24051
replace margin = 12.5 if constituencynumber == 24052
replace margin = 35.9 if constituencynumber == 24053
replace margin = 10.6 if constituencynumber == 24054
replace margin = 26.1 if constituencynumber == 24055
replace margin = 38.7 if constituencynumber == 24056
replace margin = 32 if constituencynumber == 24057
replace margin = 21.9 if constituencynumber == 24058
replace margin = 1.9 if constituencynumber == 24059
replace margin = 7.4 if constituencynumber == 24060
replace margin = 6.9 if constituencynumber == 24061
replace margin = 2.9 if constituencynumber == 24062
replace margin = 1.9 if constituencynumber == 24063
replace margin = 28.1 if constituencynumber == 24064
replace margin = 17.4 if constituencynumber == 24065
replace margin = 1.1 if constituencynumber == 24066
replace margin = 4.1 if constituencynumber == 24067
replace margin = 42.1 if constituencynumber == 24068
replace margin = 49.9 if constituencynumber == 24069
replace margin = 20.7 if constituencynumber == 24070
replace margin = 1.3 if constituencynumber == 24071
replace margin = 15.3 if constituencynumber == 24072
replace margin = 7.6 if constituencynumber == 24073
replace margin = 24.3 if constituencynumber == 24074
replace margin = 5 if constituencynumber == 24075
replace margin = 1.6 if constituencynumber == 24076
replace margin = 27.4 if constituencynumber == 24077
replace margin = 25.2 if constituencynumber == 24078
replace margin = 21.5 if constituencynumber == 35001
replace margin = 5.8 if constituencynumber == 35002
replace margin = 2.1 if constituencynumber == 35003
replace margin = 9.3 if constituencynumber == 35004
replace margin = 0.2 if constituencynumber == 35005
replace margin = 16.5 if constituencynumber == 35006
replace margin = 18.6 if constituencynumber == 35007
replace margin = 15 if constituencynumber == 35008
replace margin = 28.8 if constituencynumber == 35009
replace margin = 15.4 if constituencynumber == 35010
replace margin = 17.1 if constituencynumber == 35011
replace margin = 25.8 if constituencynumber == 35012
replace margin = 10.2 if constituencynumber == 35013
replace margin = 7.8 if constituencynumber == 35014
replace margin = 3.5 if constituencynumber == 35015
replace margin = 4.5 if constituencynumber == 35016
replace margin = 4.5 if constituencynumber == 35017
replace margin = 2.9 if constituencynumber == 35018
replace margin = 28.6 if constituencynumber == 35019
replace margin = 13.6 if constituencynumber == 35020
replace margin = 16.2 if constituencynumber == 35021
replace margin = 7.2 if constituencynumber == 35022
replace margin = 9.4 if constituencynumber == 35023
replace margin = 6.3 if constituencynumber == 35024
replace margin = 18.2 if constituencynumber == 35025
replace margin = 5.7 if constituencynumber == 35026
replace margin = 15.4 if constituencynumber == 35027
replace margin = 21.2 if constituencynumber == 35028
replace margin = 39.4 if constituencynumber == 35029
replace margin = 4.3 if constituencynumber == 35030
replace margin = 16.9 if constituencynumber == 35031
replace margin = 22.8 if constituencynumber == 35032
replace margin = 7.5 if constituencynumber == 35033
replace margin = 13.1 if constituencynumber == 35034
replace margin = 12.2 if constituencynumber == 35035
replace margin = 6.3 if constituencynumber == 35036
replace margin = 2.4 if constituencynumber == 35037
replace margin = 15.9 if constituencynumber == 35038
replace margin = 0.5 if constituencynumber == 35039
replace margin = 5.2 if constituencynumber == 35040
replace margin = 12.1 if constituencynumber == 35041
replace margin = 1.6 if constituencynumber == 35042
replace margin = 3.2 if constituencynumber == 35043
replace margin = 32.7 if constituencynumber == 35044
replace margin = 18.4 if constituencynumber == 35045
replace margin = 0.5 if constituencynumber == 35046
replace margin = 5.6 if constituencynumber == 35047
replace margin = 20.8 if constituencynumber == 35048
replace margin = 14.1 if constituencynumber == 35049
replace margin = 6.8 if constituencynumber == 35050
replace margin = 6.3 if constituencynumber == 35051
replace margin = 19.4 if constituencynumber == 35052
replace margin = 10.5 if constituencynumber == 35053
replace margin = 6.4 if constituencynumber == 35054
replace margin = 23.4 if constituencynumber == 35055
replace margin = 6 if constituencynumber == 35056
replace margin = 4.9 if constituencynumber == 35057
replace margin = 21.1 if constituencynumber == 35058
replace margin = 18.9 if constituencynumber == 35059
replace margin = 10.5 if constituencynumber == 35060
replace margin = 6.5 if constituencynumber == 35061
replace margin = 32.7 if constituencynumber == 35062
replace margin = 7.4 if constituencynumber == 35063
replace margin = 16.3 if constituencynumber == 35064
replace margin = 2.6 if constituencynumber == 35065
replace margin = 4.2 if constituencynumber == 35066
replace margin = 7.6 if constituencynumber == 35067
replace margin = 16.1 if constituencynumber == 35068
replace margin = 5 if constituencynumber == 35069
replace margin = 22.6 if constituencynumber == 35070
replace margin = 3 if constituencynumber == 35071
replace margin = 6.9 if constituencynumber == 35072
replace margin = 3.4 if constituencynumber == 35073
replace margin = 6.3 if constituencynumber == 35074
replace margin = 4.1 if constituencynumber == 35075
replace margin = 29.1 if constituencynumber == 35076
replace margin = 35.8 if constituencynumber == 35077
replace margin = 38.3 if constituencynumber == 35078
replace margin = 25.9 if constituencynumber == 35079
replace margin = 13.5 if constituencynumber == 35080
replace margin = 1.8 if constituencynumber == 35081
replace margin = 4.4 if constituencynumber == 35082
replace margin = 5.4 if constituencynumber == 35083
replace margin = 8.8 if constituencynumber == 35084
replace margin = 12.1 if constituencynumber == 35085
replace margin = 13.2 if constituencynumber == 35086
replace margin = 3.6 if constituencynumber == 35087
replace margin = 3.1 if constituencynumber == 35088
replace margin = 5.6 if constituencynumber == 35089
replace margin = 28.3 if constituencynumber == 35090
replace margin = 7.7 if constituencynumber == 35091
replace margin = 13.6 if constituencynumber == 35092
replace margin = 13.9 if constituencynumber == 35093
replace margin = 17.9 if constituencynumber == 35094
replace margin = 33.5 if constituencynumber == 35095
replace margin = 20.8 if constituencynumber == 35096
replace margin = 32.9 if constituencynumber == 35097
replace margin = 28.7 if constituencynumber == 35098
replace margin = 8 if constituencynumber == 35099
replace margin = 3.7 if constituencynumber == 35100
replace margin = 27.4 if constituencynumber == 35101
replace margin = 12.5 if constituencynumber == 35102
replace margin = 19.6 if constituencynumber == 35103
replace margin = 24.8 if constituencynumber == 35104
replace margin = 14.4 if constituencynumber == 35105
replace margin = 21.8 if constituencynumber == 35106
replace margin = 8.1 if constituencynumber == 35107
replace margin = 31.3 if constituencynumber == 35108
replace margin = 2.2 if constituencynumber == 35109
replace margin = 21.2 if constituencynumber == 35110
replace margin = 4.9 if constituencynumber == 35111
replace margin = 17.4 if constituencynumber == 35112
replace margin = 14.4 if constituencynumber == 35113
replace margin = 2.9 if constituencynumber == 35114
replace margin = 16.4 if constituencynumber == 35115
replace margin = 16 if constituencynumber == 35116
replace margin = 26.1 if constituencynumber == 35117
replace margin = 2.9 if constituencynumber == 35118
replace margin = 12.5 if constituencynumber == 35119
replace margin = 15.6 if constituencynumber == 35120
replace margin = 46.8 if constituencynumber == 35121
replace margin = 13 if constituencynumber == 46001
replace margin = 13 if constituencynumber == 46002
replace margin = 3 if constituencynumber == 46003
replace margin = 16.8 if constituencynumber == 46004
replace margin = 0.1 if constituencynumber == 46005
replace margin = 2.8 if constituencynumber == 46006
replace margin = 35.1 if constituencynumber == 46007
replace margin = 21.4 if constituencynumber == 46008
replace margin = 29.8 if constituencynumber == 46009
replace margin = 20.5 if constituencynumber == 46010
replace margin = 26.5 if constituencynumber == 46011
replace margin = 53.6 if constituencynumber == 46012
replace margin = 23.6 if constituencynumber == 46013
replace margin = 31.5 if constituencynumber == 46014
replace margin = 43.4 if constituencynumber == 47001
replace margin = 54.3 if constituencynumber == 47002
replace margin = 0.3 if constituencynumber == 47003
replace margin = 46.1 if constituencynumber == 47004
replace margin = 31.7 if constituencynumber == 47005
replace margin = 21.3 if constituencynumber == 47006
replace margin = 0.3 if constituencynumber == 47007
replace margin = 14.5 if constituencynumber == 47008
replace margin = 24.9 if constituencynumber == 47009
replace margin = 11.4 if constituencynumber == 47010
replace margin = 10 if constituencynumber == 47011
replace margin = 6.7 if constituencynumber == 47012
replace margin = 56.5 if constituencynumber == 47013
replace margin = 39 if constituencynumber == 47014
replace margin = 37.3 if constituencynumber == 48001
replace margin = 71.5 if constituencynumber == 48002
replace margin = 63.7 if constituencynumber == 48003
replace margin = 1.2 if constituencynumber == 48004
replace margin = 2.4 if constituencynumber == 48005
replace margin = 12 if constituencynumber == 48006
replace margin = 37.8 if constituencynumber == 48007
replace margin = 44.1 if constituencynumber == 48008
replace margin = 33.2 if constituencynumber == 48009
replace margin = 28.7 if constituencynumber == 48010
replace margin = 41.2 if constituencynumber == 48011
replace margin = 30 if constituencynumber == 48012
replace margin = 6.1 if constituencynumber == 48013
replace margin = 2.2 if constituencynumber == 48014
replace margin = 5.9 if constituencynumber == 48015
replace margin = 17.7 if constituencynumber == 48016
replace margin = 0.2 if constituencynumber == 48017
replace margin = 19.7 if constituencynumber == 48018
replace margin = 12.7 if constituencynumber == 48019
replace margin = 14.4 if constituencynumber == 48020
replace margin = 44.3 if constituencynumber == 48021
replace margin = 62.3 if constituencynumber == 48022
replace margin = 32.2 if constituencynumber == 48023
replace margin = 58.3 if constituencynumber == 48024
replace margin = 59.1 if constituencynumber == 48025
replace margin = 36.3 if constituencynumber == 48026
replace margin = 50.9 if constituencynumber == 48027
replace margin = 55 if constituencynumber == 48028
replace margin = 60.9 if constituencynumber == 48029
replace margin = 55.7 if constituencynumber == 48030
replace margin = 22.7 if constituencynumber == 48031
replace margin = 43.5 if constituencynumber == 48032
replace margin = 54.7 if constituencynumber == 48033
replace margin = 58 if constituencynumber == 48034
replace margin = 15.5 if constituencynumber == 59001
replace margin = 6.5 if constituencynumber == 59002
replace margin = 1.2 if constituencynumber == 59003
replace margin = 5.1 if constituencynumber == 59004
replace margin = 2.4 if constituencynumber == 59005
replace margin = 8.5 if constituencynumber == 59006
replace margin = 10.8 if constituencynumber == 59007
replace margin = 3.3 if constituencynumber == 59008
replace margin = 9.8 if constituencynumber == 59009
replace margin = 12.2 if constituencynumber == 59010
replace margin = 16.3 if constituencynumber == 59011
replace margin = 17.6 if constituencynumber == 59012
replace margin = 4.5 if constituencynumber == 59013
replace margin = 6.4 if constituencynumber == 59014
replace margin = 0.4 if constituencynumber == 59015
replace margin = 9.1 if constituencynumber == 59016
replace margin = 2.3 if constituencynumber == 59017
replace margin = 9.7 if constituencynumber == 59018
replace margin = 14.5 if constituencynumber == 59019
replace margin = 9.4 if constituencynumber == 59020
replace margin = 29.8 if constituencynumber == 59021
replace margin = 2.5 if constituencynumber == 59022
replace margin = 5.2 if constituencynumber == 59023
replace margin = 27.6 if constituencynumber == 59024
replace margin = 2.8 if constituencynumber == 59025
replace margin = 7.7 if constituencynumber == 59026
replace margin = 34.9 if constituencynumber == 59027
replace margin = 26.3 if constituencynumber == 59028
replace margin = 7.4 if constituencynumber == 59029
replace margin = 2.5 if constituencynumber == 59030
replace margin = 6.6 if constituencynumber == 59031
replace margin = 15 if constituencynumber == 59032
replace margin = 29.9 if constituencynumber == 59033
replace margin = 36.1 if constituencynumber == 59034
replace margin = 21.8 if constituencynumber == 59035
replace margin = 17.1 if constituencynumber == 59036
replace margin = 14 if constituencynumber == 59037
replace margin = 17.9 if constituencynumber == 59038
replace margin = 32.9 if constituencynumber == 59039
replace margin = 14.9 if constituencynumber == 59040
replace margin = 9.4 if constituencynumber == 59041
replace margin = 28.4 if constituencynumber == 59042
replace margin = 29.4 if constituencynumber == 60001
replace margin = 17.9 if constituencynumber == 61001
replace margin = 20.5 if constituencynumber == 62001

* Effective number of electoral parties by district

gen enep = .

replace enep = 2.650755068 if constituencynumber == 10001
replace enep = 1.460455253 if constituencynumber == 10002
replace enep = 1.675664988 if constituencynumber == 10003
replace enep = 1.799917564 if constituencynumber == 10004
replace enep = 1.740634949 if constituencynumber == 10005
replace enep = 2.338344405 if constituencynumber == 10006
replace enep = 2.114857934 if constituencynumber == 10007
replace enep = 2.149012529 if constituencynumber == 11001
replace enep = 2.527818644 if constituencynumber == 11002
replace enep = 2.742062415 if constituencynumber == 11003
replace enep = 2.285061411 if constituencynumber == 11004
replace enep = 1.718520148 if constituencynumber == 12001
replace enep = 2.375054329 if constituencynumber == 12002
replace enep = 2.08090994 if constituencynumber == 12003
replace enep = 2.386543712 if constituencynumber == 12004
replace enep = 2.462453737 if constituencynumber == 12005
replace enep = 1.959335942 if constituencynumber == 12006
replace enep = 1.852791972 if constituencynumber == 12007
replace enep = 2.690189685 if constituencynumber == 12008
replace enep = 2.475639705 if constituencynumber == 12009
replace enep = 1.770281227 if constituencynumber == 12010
replace enep = 2.121300187 if constituencynumber == 12011
replace enep = 2.388972503 if constituencynumber == 13001
replace enep = 1.945820572 if constituencynumber == 13002
replace enep = 2.866298634 if constituencynumber == 13003
replace enep = 2.96644359 if constituencynumber == 13004
replace enep = 2.469648026 if constituencynumber == 13005
replace enep = 2.732285229 if constituencynumber == 13006
replace enep = 2.446968084 if constituencynumber == 13007
replace enep = 2.777824075 if constituencynumber == 13008
replace enep = 2.756681507 if constituencynumber == 13009
replace enep = 2.706902059 if constituencynumber == 13010
replace enep = 3.528668669 if constituencynumber == 24001
replace enep = 3.304605298 if constituencynumber == 24002
replace enep = 3.009845204 if constituencynumber == 24003
replace enep = 3.327853468 if constituencynumber == 24004
replace enep = 3.369646895 if constituencynumber == 24005
replace enep = 3.872501752 if constituencynumber == 24006
replace enep = 2.428221765 if constituencynumber == 24007
replace enep = 4.06944094 if constituencynumber == 24008
replace enep = 3.553470853 if constituencynumber == 24009
replace enep = 2.987973407 if constituencynumber == 24010
replace enep = 3.724491886 if constituencynumber == 24011
replace enep = 3.372487918 if constituencynumber == 24012
replace enep = 3.890218047 if constituencynumber == 24013
replace enep = 4.191764859 if constituencynumber == 24014
replace enep = 2.82843825 if constituencynumber == 24015
replace enep = 3.365484933 if constituencynumber == 24016
replace enep = 2.932568519 if constituencynumber == 24017
replace enep = 3.254191399 if constituencynumber == 24018
replace enep = 3.47316287 if constituencynumber == 24019
replace enep = 3.918065416 if constituencynumber == 24020
replace enep = 3.588190547 if constituencynumber == 24021
replace enep = 3.922306944 if constituencynumber == 24022
replace enep = 3.702222815 if constituencynumber == 24023
replace enep = 2.698858653 if constituencynumber == 24024
replace enep = 4.046305925 if constituencynumber == 24025
replace enep = 3.300765778 if constituencynumber == 24026
replace enep = 2.659369357 if constituencynumber == 24027
replace enep = 3.740541107 if constituencynumber == 24028
replace enep = 2.647800208 if constituencynumber == 24029
replace enep = 2.673982683 if constituencynumber == 24030
replace enep = 3.741744776 if constituencynumber == 24031
replace enep = 4.006426308 if constituencynumber == 24032
replace enep = 3.65817969 if constituencynumber == 24033
replace enep = 3.718467396 if constituencynumber == 24034
replace enep = 1.490464011 if constituencynumber == 24035
replace enep = 2.178122502 if constituencynumber == 24036
replace enep = 3.210211039 if constituencynumber == 24037
replace enep = 3.697910311 if constituencynumber == 24038
replace enep = 3.468958029 if constituencynumber == 24039
replace enep = 3.172910955 if constituencynumber == 24040
replace enep = 3.753767844 if constituencynumber == 24041
replace enep = 3.000138006 if constituencynumber == 24042
replace enep = 3.702661473 if constituencynumber == 24043
replace enep = 3.850344798 if constituencynumber == 24044
replace enep = 2.96672521 if constituencynumber == 24045
replace enep = 3.350139868 if constituencynumber == 24046
replace enep = 3.727907488 if constituencynumber == 24047
replace enep = 3.722564698 if constituencynumber == 24048
replace enep = 3.848107501 if constituencynumber == 24049
replace enep = 3.657724749 if constituencynumber == 24050
replace enep = 4.006956076 if constituencynumber == 24051
replace enep = 2.476246604 if constituencynumber == 24052
replace enep = 2.482621648 if constituencynumber == 24053
replace enep = 3.085238983 if constituencynumber == 24054
replace enep = 2.812812925 if constituencynumber == 24055
replace enep = 2.421805941 if constituencynumber == 24056
replace enep = 2.687962498 if constituencynumber == 24057
replace enep = 3.328462683 if constituencynumber == 24058
replace enep = 4.164185506 if constituencynumber == 24059
replace enep = 3.82187027 if constituencynumber == 24060
replace enep = 4.010411027 if constituencynumber == 24061
replace enep = 3.732025632 if constituencynumber == 24062
replace enep = 3.695873926 if constituencynumber == 24063
replace enep = 3.008849025 if constituencynumber == 24064
replace enep = 3.519676753 if constituencynumber == 24065
replace enep = 4.064429334 if constituencynumber == 24066
replace enep = 3.7242006 if constituencynumber == 24067
replace enep = 2.306805075 if constituencynumber == 24068
replace enep = 2.181734086 if constituencynumber == 24069
replace enep = 3.580482076 if constituencynumber == 24070
replace enep = 3.7236043 if constituencynumber == 24071
replace enep = 3.64281473 if constituencynumber == 24072
replace enep = 3.576768175 if constituencynumber == 24073
replace enep = 3.237157387 if constituencynumber == 24074
replace enep = 3.760769905 if constituencynumber == 24075
replace enep = 3.903977763 if constituencynumber == 24076
replace enep = 2.970055896 if constituencynumber == 24077
replace enep = 3.289581894 if constituencynumber == 24078
replace enep = 2.284471533 if constituencynumber == 35001
replace enep = 3.010823912 if constituencynumber == 35002
replace enep = 2.317626009 if constituencynumber == 35003
replace enep = 2.713807309 if constituencynumber == 35004
replace enep = 2.775172199 if constituencynumber == 35005
replace enep = 2.565029921 if constituencynumber == 35006
replace enep = 2.728498071 if constituencynumber == 35007
replace enep = 2.680799843 if constituencynumber == 35008
replace enep = 2.619343854 if constituencynumber == 35009
replace enep = 2.697264434 if constituencynumber == 35010
replace enep = 2.463818821 if constituencynumber == 35011
replace enep = 2.388265021 if constituencynumber == 35012
replace enep = 3.088974831 if constituencynumber == 35013
replace enep = 2.617506406 if constituencynumber == 35014
replace enep = 2.493255743 if constituencynumber == 35015
replace enep = 2.808594299 if constituencynumber == 35016
replace enep = 2.883024177 if constituencynumber == 35017
replace enep = 2.632472603 if constituencynumber == 35018
replace enep = 2.321047257 if constituencynumber == 35019
replace enep = 2.410864319 if constituencynumber == 35020
replace enep = 2.300172513 if constituencynumber == 35021
replace enep = 2.646146681 if constituencynumber == 35022
replace enep = 2.794654385 if constituencynumber == 35023
replace enep = 2.354037881 if constituencynumber == 35024
replace enep = 2.75534468 if constituencynumber == 35025
replace enep = 2.91640419 if constituencynumber == 35026
replace enep = 2.356345403 if constituencynumber == 35027
replace enep = 2.464389571 if constituencynumber == 35028
replace enep = 2.184130978 if constituencynumber == 35029
replace enep = 2.755868622 if constituencynumber == 35030
replace enep = 2.363027132 if constituencynumber == 35031
replace enep = 2.963129776 if constituencynumber == 35032
replace enep = 2.870989228 if constituencynumber == 35033
replace enep = 2.932001032 if constituencynumber == 35034
replace enep = 2.917067763 if constituencynumber == 35035
replace enep = 3.089051167 if constituencynumber == 35036
replace enep = 3.245846939 if constituencynumber == 35037
replace enep = 2.8012303 if constituencynumber == 35038
replace enep = 2.685955408 if constituencynumber == 35039
replace enep = 2.654730996 if constituencynumber == 35040
replace enep = 2.368624256 if constituencynumber == 35041
replace enep = 3.101236773 if constituencynumber == 35042
replace enep = 2.355013588 if constituencynumber == 35043
replace enep = 2.56808639 if constituencynumber == 35044
replace enep = 2.784111632 if constituencynumber == 35045
replace enep = 2.622902661 if constituencynumber == 35046
replace enep = 2.944788167 if constituencynumber == 35047
replace enep = 2.714227165 if constituencynumber == 35048
replace enep = 2.74092753 if constituencynumber == 35049
replace enep = 2.512960594 if constituencynumber == 35050
replace enep = 3.161145726 if constituencynumber == 35051
replace enep = 2.669122259 if constituencynumber == 35052
replace enep = 2.799685315 if constituencynumber == 35053
replace enep = 2.329210631 if constituencynumber == 35054
replace enep = 2.346475945 if constituencynumber == 35055
replace enep = 2.300913463 if constituencynumber == 35056
replace enep = 2.618884251 if constituencynumber == 35057
replace enep = 2.371837747 if constituencynumber == 35058
replace enep = 2.343511987 if constituencynumber == 35059
replace enep = 2.440423169 if constituencynumber == 35060
replace enep = 2.473545432 if constituencynumber == 35061
replace enep = 2.301919801 if constituencynumber == 35062
replace enep = 2.497789456 if constituencynumber == 35063
replace enep = 2.426071535 if constituencynumber == 35064
replace enep = 2.53968254 if constituencynumber == 35065
replace enep = 3.169692667 if constituencynumber == 35066
replace enep = 2.936236684 if constituencynumber == 35067
replace enep = 2.777546316 if constituencynumber == 35068
replace enep = 2.820190306 if constituencynumber == 35069
replace enep = 2.616910475 if constituencynumber == 35070
replace enep = 2.775418672 if constituencynumber == 35071
replace enep = 2.33303626 if constituencynumber == 35072
replace enep = 2.432267433 if constituencynumber == 35073
replace enep = 3.097509602 if constituencynumber == 35074
replace enep = 2.836557554 if constituencynumber == 35075
replace enep = 2.19222287 if constituencynumber == 35076
replace enep = 2.300950523 if constituencynumber == 35077
replace enep = 2.462144528 if constituencynumber == 35078
replace enep = 2.421207844 if constituencynumber == 35079
replace enep = 2.928909508 if constituencynumber == 35080
replace enep = 2.809612245 if constituencynumber == 35081
replace enep = 2.82323181 if constituencynumber == 35082
replace enep = 2.86669304 if constituencynumber == 35083
replace enep = 2.853050767 if constituencynumber == 35084
replace enep = 2.451425013 if constituencynumber == 35085
replace enep = 2.971326697 if constituencynumber == 35086
replace enep = 2.414648222 if constituencynumber == 35087
replace enep = 2.405349497 if constituencynumber == 35088
replace enep = 2.809762238 if constituencynumber == 35089
replace enep = 2.49241061 if constituencynumber == 35090
replace enep = 3.10002542 if constituencynumber == 35091
replace enep = 2.895244272 if constituencynumber == 35092
replace enep = 2.379468046 if constituencynumber == 35093
replace enep = 2.653843296 if constituencynumber == 35094
replace enep = 2.256241328 if constituencynumber == 35095
replace enep = 2.805064825 if constituencynumber == 35096
replace enep = 2.228680443 if constituencynumber == 35097
replace enep = 2.649610242 if constituencynumber == 35098
replace enep = 2.649933222 if constituencynumber == 35099
replace enep = 2.769722502 if constituencynumber == 35100
replace enep = 2.507315092 if constituencynumber == 35101
replace enep = 2.400649136 if constituencynumber == 35102
replace enep = 2.878542767 if constituencynumber == 35103
replace enep = 2.170996357 if constituencynumber == 35104
replace enep = 3.039199596 if constituencynumber == 35105
replace enep = 3.271394923 if constituencynumber == 35106
replace enep = 2.8862861 if constituencynumber == 35107
replace enep = 2.37203466 if constituencynumber == 35108
replace enep = 2.831994744 if constituencynumber == 35109
replace enep = 2.767821309 if constituencynumber == 35110
replace enep = 2.312673451 if constituencynumber == 35111
replace enep = 2.671054319 if constituencynumber == 35112
replace enep = 2.49498508 if constituencynumber == 35113
replace enep = 2.558526289 if constituencynumber == 35114
replace enep = 2.339372814 if constituencynumber == 35115
replace enep = 2.975915913 if constituencynumber == 35116
replace enep = 2.699237465 if constituencynumber == 35117
replace enep = 2.385416518 if constituencynumber == 35118
replace enep = 2.476657503 if constituencynumber == 35119
replace enep = 2.925208275 if constituencynumber == 35120
replace enep = 1.99957609 if constituencynumber == 35121
replace enep = 2.501075462 if constituencynumber == 46001
replace enep = 2.34224401 if constituencynumber == 46002
replace enep = 2.564668106 if constituencynumber == 46003
replace enep = 3.081398215 if constituencynumber == 46004
replace enep = 3.130066796 if constituencynumber == 46005
replace enep = 2.765211428 if constituencynumber == 46006
replace enep = 2.258886459 if constituencynumber == 46007
replace enep = 2.275110969 if constituencynumber == 46008
replace enep = 2.297846917 if constituencynumber == 46009
replace enep = 2.614365415 if constituencynumber == 46010
replace enep = 2.547465654 if constituencynumber == 46011
replace enep = 1.935501353 if constituencynumber == 46012
replace enep = 2.158712889 if constituencynumber == 46013
replace enep = 2.247221311 if constituencynumber == 46014
replace enep = 2.316884994 if constituencynumber == 47001
replace enep = 1.925973291 if constituencynumber == 47002
replace enep = 3.097797466 if constituencynumber == 47003
replace enep = 2.10615883 if constituencynumber == 47004
replace enep = 2.514945061 if constituencynumber == 47005
replace enep = 2.71154086 if constituencynumber == 47006
replace enep = 3.108582797 if constituencynumber == 47007
replace enep = 2.910987815 if constituencynumber == 47008
replace enep = 2.428793837 if constituencynumber == 47009
replace enep = 2.991504128 if constituencynumber == 47010
replace enep = 2.983444864 if constituencynumber == 47011
replace enep = 3.072611966 if constituencynumber == 47012
replace enep = 1.890105487 if constituencynumber == 47013
replace enep = 2.359959975 if constituencynumber == 47014
replace enep = 2.100200569 if constituencynumber == 48001
replace enep = 1.495738641 if constituencynumber == 48002
replace enep = 1.610217798 if constituencynumber == 48003
replace enep = 2.352553697 if constituencynumber == 48004
replace enep = 2.463205862 if constituencynumber == 48005
replace enep = 2.695323344 if constituencynumber == 48006
replace enep = 2.08144269 if constituencynumber == 48007
replace enep = 1.98954692 if constituencynumber == 48008
replace enep = 2.26713672 if constituencynumber == 48009
replace enep = 2.131696191 if constituencynumber == 48010
replace enep = 1.997642782 if constituencynumber == 48011
replace enep = 2.15458269 if constituencynumber == 48012
replace enep = 2.655365431 if constituencynumber == 48013
replace enep = 3.109269042 if constituencynumber == 48014
replace enep = 3.092193757 if constituencynumber == 48015
replace enep = 2.9692093 if constituencynumber == 48016
replace enep = 2.810899544 if constituencynumber == 48017
replace enep = 2.702856108 if constituencynumber == 48018
replace enep = 2.984940973 if constituencynumber == 48019
replace enep = 2.616766671 if constituencynumber == 48020
replace enep = 2.044254011 if constituencynumber == 48021
replace enep = 1.677078529 if constituencynumber == 48022
replace enep = 2.201397007 if constituencynumber == 48023
replace enep = 1.783507196 if constituencynumber == 48024
replace enep = 1.786977934 if constituencynumber == 48025
replace enep = 2.501657348 if constituencynumber == 48026
replace enep = 1.93960083 if constituencynumber == 48027
replace enep = 1.925068629 if constituencynumber == 48028
replace enep = 1.730669717 if constituencynumber == 48029
replace enep = 1.86502445 if constituencynumber == 48030
replace enep = 3.262876944 if constituencynumber == 48031
replace enep = 2.170162437 if constituencynumber == 48032
replace enep = 1.893362062 if constituencynumber == 48033
replace enep = 1.811298882 if constituencynumber == 48034
replace enep = 2.761317259 if constituencynumber == 59001
replace enep = 3.354849099 if constituencynumber == 59002
replace enep = 3.200327714 if constituencynumber == 59003
replace enep = 3.320064144 if constituencynumber == 59004
replace enep = 2.994280923 if constituencynumber == 59005
replace enep = 3.043454443 if constituencynumber == 59006
replace enep = 2.821200759 if constituencynumber == 59007
replace enep = 3.297815857 if constituencynumber == 59008
replace enep = 3.49770201 if constituencynumber == 59009
replace enep = 3.757900987 if constituencynumber == 59010
replace enep = 2.688967168 if constituencynumber == 59011
replace enep = 2.835986399 if constituencynumber == 59012
replace enep = 3.193000942 if constituencynumber == 59013
replace enep = 2.552785216 if constituencynumber == 59014
replace enep = 3.163976232 if constituencynumber == 59015
replace enep = 2.781378117 if constituencynumber == 59016
replace enep = 3.27641115 if constituencynumber == 59017
replace enep = 3.854856927 if constituencynumber == 59018
replace enep = 3.162365323 if constituencynumber == 59019
replace enep = 3.204203916 if constituencynumber == 59020
replace enep = 2.458041236 if constituencynumber == 59021
replace enep = 3.300569678 if constituencynumber == 59022
replace enep = 3.19217534 if constituencynumber == 59023
replace enep = 2.74305527 if constituencynumber == 59024
replace enep = 2.625871133 if constituencynumber == 59025
replace enep = 3.734046287 if constituencynumber == 59026
replace enep = 2.701629353 if constituencynumber == 59027
replace enep = 2.783855863 if constituencynumber == 59028
replace enep = 3.239453958 if constituencynumber == 59029
replace enep = 2.646720977 if constituencynumber == 59030
replace enep = 2.719815488 if constituencynumber == 59031
replace enep = 2.98856277 if constituencynumber == 59032
replace enep = 2.457884155 if constituencynumber == 59033
replace enep = 2.58552931 if constituencynumber == 59034
replace enep = 2.86717798 if constituencynumber == 59035
replace enep = 2.992542584 if constituencynumber == 59036
replace enep = 3.311291172 if constituencynumber == 59037
replace enep = 3.016354675 if constituencynumber == 59038
replace enep = 2.354137632 if constituencynumber == 59039
replace enep = 2.678286861 if constituencynumber == 59040
replace enep = 3.173797131 if constituencynumber == 59041
replace enep = 2.600814055 if constituencynumber == 59042
replace enep = 2.592486973 if constituencynumber == 60001
replace enep = 2.77325583 if constituencynumber == 61001
replace enep = 2.822259727 if constituencynumber == 62001

* Boundary changes

gen boundary = .

replace boundary = 0 if constituencyname == "Labrador"
replace boundary = 0 if constituencyname == "Cardigan"
replace boundary = 0 if constituencyname == "Charlottetown"
replace boundary = 0 if constituencyname == "Egmont"
replace boundary = 0 if constituencyname == "Malpeque"
replace boundary = 0 if constituencyname == "Kings--Hants"
replace boundary = 0 if constituencyname == "Sydney--Victoria"
replace boundary = 0 if constituencyname == "West Nova"
replace boundary = 0 if constituencyname == "B?cancour--Nicolet--Saurel"
replace boundary = 0 if constituencyname == "Rimouski-Neigette--Temiscouata--Les Basques"
replace boundary = 0 if constituencyname == "Drummond"
replace boundary = 0 if constituencyname == "Laurentides--Labelle"
replace boundary = 0 if constituencyname == "Montmagny--L'Islet--Kamouraska--Rivi?re-du-Loup"
replace boundary = 0 if constituencyname == "Pierrefonds--Dollard"
replace boundary = 0 if constituencyname == "Portneuf--Jacques-Cartier"
replace boundary = 0 if constituencyname == "Saint-Hyacinthe--Bagot"
replace boundary = 0 if constituencyname == "Saint-Jean"
replace boundary = 0 if constituencyname == "Beaches--East York"
replace boundary = 0 if constituencyname == "Bruce--Grey--Owen Sound"
replace boundary = 0 if constituencyname == "Davenport"
replace boundary = 0 if constituencyname == "Dufferin--Caledon"
replace boundary = 0 if constituencyname == "Eglinton--Lawrence"
replace boundary = 0 if constituencyname == "Guelph"
replace boundary = 0 if constituencyname == "Huron--Bruce"
replace boundary = 0 if constituencyname == "Leeds-Grenville-Thousand Islands and Rideau Lakes"
replace boundary = 0 if constituencyname == "Niagara Falls"
replace boundary = 0 if constituencyname == "Oakville"
replace boundary = 0 if constituencyname == "Parkdale--High Park"
replace boundary = 0 if constituencyname == "Parry Sound--Muskoka"
replace boundary = 0 if constituencyname == "Perth--Wellington"
replace boundary = 0 if constituencyname == "Renfrew--Nipissing--Pembroke"
replace boundary = 0 if constituencyname == "Sarnia--Lambton"
replace boundary = 0 if constituencyname == "Stormont--Dundas--South Glengarry"
replace boundary = 0 if constituencyname == "Toronto--Danforth"
replace boundary = 0 if constituencyname == "Wellington--Halton Hills"
replace boundary = 0 if constituencyname == "York South--Weston"
replace boundary = 0 if constituencyname == "Humber River--Black Creek"
replace boundary = 0 if constituencyname == "Charleswood--St. James--Assiniboia--Headingley"
replace boundary = 0 if constituencyname == "Winnipeg Centre"
replace boundary = 0 if constituencyname == "Vancouver East"
replace boundary = 0 if constituencyname == "Victoria"
replace boundary = 0 if constituencyname == "Yukon"
replace boundary = 0 if constituencyname == "Western Arctic"
replace boundary = 0 if constituencyname == "Nunavut"

replace boundary = 1 if boundary!=0 & constituencynumber!=.

* Competition measure

gen competition = .

replace competition = .4283795 if constituencynumber == 10001
replace competition = .2522675 if constituencynumber == 10002
replace competition = .3003395 if constituencynumber == 10003
replace competition = .325148 if constituencynumber == 10004
replace competition = .3233915 if constituencynumber == 10005
replace competition = .119774 if constituencynumber == 10006
replace competition = .2300315 if constituencynumber == 10007
replace competition = .389268 if constituencynumber == 11001
replace competition = .39063 if constituencynumber == 11002
replace competition = .3456155 if constituencynumber == 11003
replace competition = .3995195 if constituencynumber == 11004
replace competition = .31928 if constituencynumber == 12001
replace competition = .3663995 if constituencynumber == 12002
replace competition = .331228 if constituencynumber == 12003
replace competition = .369294 if constituencynumber == 12004
replace competition = .270212 if constituencynumber == 12005
replace competition = .357988 if constituencynumber == 12006
replace competition = .3380255 if constituencynumber == 12007
replace competition = .29711 if constituencynumber == 12008
replace competition = .3833255 if constituencynumber == 12009
replace competition = .3297835 if constituencynumber == 12010
replace competition = .3384015 if constituencynumber == 12011
replace competition = .2200335 if constituencynumber == 13001
replace competition = .3585955 if constituencynumber == 13002
replace competition = .3779935 if constituencynumber == 13003
replace competition = .269632 if constituencynumber == 13004
replace competition = .358264 if constituencynumber == 13005
replace competition = .29902 if constituencynumber == 13006
replace competition = .3894455 if constituencynumber == 13007
replace competition = .2515495 if constituencynumber == 13008
replace competition = .3365335 if constituencynumber == 13009
replace competition = .272852 if constituencynumber == 13010
replace competition = .3593135 if constituencynumber == 24001
replace competition = .3766535 if constituencynumber == 24002
replace competition = .37445 if constituencynumber == 24003
replace competition = .4315495 if constituencynumber == 24004
replace competition = .4248315 if constituencynumber == 24005
replace competition = .4749755 if constituencynumber == 24006
replace competition = .397216 if constituencynumber == 24007
replace competition = .4140035 if constituencynumber == 24008
replace competition = .4243455 if constituencynumber == 24009
replace competition = .44421 if constituencynumber == 24010
replace competition = .355294 if constituencynumber == 24011
replace competition = .410686 if constituencynumber == 24012
replace competition = .396682 if constituencynumber == 24013
replace competition = .4009615 if constituencynumber == 24014
replace competition = .453808 if constituencynumber == 24015
replace competition = .430356 if constituencynumber == 24016
replace competition = .4164975 if constituencynumber == 24017
replace competition = .3900295 if constituencynumber == 24018
replace competition = .435274 if constituencynumber == 24019
replace competition = .409816 if constituencynumber == 24020
replace competition = .4200735 if constituencynumber == 24021
replace competition = .37944 if constituencynumber == 24022
replace competition = .4060935 if constituencynumber == 24023
replace competition = .4248035 if constituencynumber == 24024
replace competition = .404972 if constituencynumber == 24025
replace competition = .335696 if constituencynumber == 24026
replace competition = .388838 if constituencynumber == 24027
replace competition = .35123 if constituencynumber == 24028
replace competition = .4382055 if constituencynumber == 24029
replace competition = .3461435 if constituencynumber == 24030
replace competition = .3794675 if constituencynumber == 24031
replace competition = .3838055 if constituencynumber == 24032
replace competition = .366822 if constituencynumber == 24033
replace competition = .4097895 if constituencynumber == 24034
replace competition = .392524 if constituencynumber == 24035
replace competition = .3859955 if constituencynumber == 24036
replace competition = .3868295 if constituencynumber == 24037
replace competition = .354948 if constituencynumber == 24038
replace competition = .378228 if constituencynumber == 24039
replace competition = .4483015 if constituencynumber == 24040
replace competition = .401306 if constituencynumber == 24041
replace competition = .436038 if constituencynumber == 24042
replace competition = .348464 if constituencynumber == 24043
replace competition = .408886 if constituencynumber == 24044
replace competition = .4340655 if constituencynumber == 24045
replace competition = .3845675 if constituencynumber == 24046
replace competition = .3929735 if constituencynumber == 24047
replace competition = .353088 if constituencynumber == 24048
replace competition = .3861735 if constituencynumber == 24049
replace competition = .3972995 if constituencynumber == 24050
replace competition = .38205 if constituencynumber == 24051
replace competition = .250656 if constituencynumber == 24052
replace competition = .4003175 if constituencynumber == 24053
replace competition = .347096 if constituencynumber == 24054
replace competition = .4062075 if constituencynumber == 24055
replace competition = .3959235 if constituencynumber == 24056
replace competition = .412792 if constituencynumber == 24057
replace competition = .4328415 if constituencynumber == 24058
replace competition = .4060755 if constituencynumber == 24059
replace competition = .404776 if constituencynumber == 24060
replace competition = .4181295 if constituencynumber == 24061
replace competition = .3651295 if constituencynumber == 24062
replace competition = .3529755 if constituencynumber == 24063
replace competition = .4294415 if constituencynumber == 24064
replace competition = .42819 if constituencynumber == 24065
replace competition = .3921035 if constituencynumber == 24066
replace competition = .3710795 if constituencynumber == 24067
replace competition = .3977915 if constituencynumber == 24068
replace competition = .3975155 if constituencynumber == 24069
replace competition = .4529415 if constituencynumber == 24070
replace competition = .350646 if constituencynumber == 24071
replace competition = .4309695 if constituencynumber == 24072
replace competition = .3817 if constituencynumber == 24073
replace competition = .4414615 if constituencynumber == 24074
replace competition = .378744 if constituencynumber == 24075
replace competition = .378822 if constituencynumber == 24076
replace competition = .434202 if constituencynumber == 24077
replace competition = .453156 if constituencynumber == 24078
replace competition = .2748735 if constituencynumber == 35001
replace competition = .28145 if constituencynumber == 35002
replace competition = .1217855 if constituencynumber == 35003
replace competition = .2717955 if constituencynumber == 35004
replace competition = .2106015 if constituencynumber == 35005
replace competition = .294932 if constituencynumber == 35006
replace competition = .33677 if constituencynumber == 35007
replace competition = .3027455 if constituencynumber == 35008
replace competition = .362036 if constituencynumber == 35009
replace competition = .308162 if constituencynumber == 35010
replace competition = .2810135 if constituencynumber == 35011
replace competition = .324252 if constituencynumber == 35012
replace competition = .323896 if constituencynumber == 35013
replace competition = .2404715 if constituencynumber == 35014
replace competition = .1779235 if constituencynumber == 35015
replace competition = .25109 if constituencynumber == 35016
replace competition = .2585495 if constituencynumber == 35017
replace competition = .2048355 if constituencynumber == 35018
replace competition = .33059 if constituencynumber == 35019
replace competition = .242566 if constituencynumber == 35020
replace competition = .239266 if constituencynumber == 35021
replace competition = .24333 if constituencynumber == 35022
replace competition = .2823255 if constituencynumber == 35023
replace competition = .1691535 if constituencynumber == 35024
replace competition = .339704 if constituencynumber == 35025
replace competition = .2698915 if constituencynumber == 35026
replace competition = .2462835 if constituencynumber == 35027
replace competition = .3119935 if constituencynumber == 35028
replace competition = .358438 if constituencynumber == 35029
replace competition = .238758 if constituencynumber == 35030
replace competition = .2590255 if constituencynumber == 35031
replace competition = .403208 if constituencynumber == 35032
replace competition = .2860255 if constituencynumber == 35033
replace competition = .32783 if constituencynumber == 35034
replace competition = .32781 if constituencynumber == 35035
replace competition = .2962674 if constituencynumber == 35036
replace competition = .294302 if constituencynumber == 35037
replace competition = .3309455 if constituencynumber == 35038
replace competition = .1924255 if constituencynumber == 35039
replace competition = .225478 if constituencynumber == 35040
replace competition = .2221455 if constituencynumber == 35041
replace competition = .260018 if constituencynumber == 35042
replace competition = .141588 if constituencynumber == 35043
replace competition = .3920775 if constituencynumber == 35044
replace competition = .345998 if constituencynumber == 35045
replace competition = .1829515 if constituencynumber == 35046
replace competition = .283124 if constituencynumber == 35047
replace competition = .346518 if constituencynumber == 35048
replace competition = .3107615 if constituencynumber == 35049
replace competition = .212166 if constituencynumber == 35050
replace competition = .304038 if constituencynumber == 35051
replace competition = .334922 if constituencynumber == 35052
replace competition = .2931495 if constituencynumber == 35053
replace competition = .16342 if constituencynumber == 35054
replace competition = .299778 if constituencynumber == 35055
replace competition = .1534915 if constituencynumber == 35056
replace competition = .218298 if constituencynumber == 35057
replace competition = .2914295 if constituencynumber == 35058
replace competition = .268436 if constituencynumber == 35059
replace competition = .2234755 if constituencynumber == 35060
replace competition = .1998895 if constituencynumber == 35061
replace competition = .3487575 if constituencynumber == 35062
replace competition = .212522 if constituencynumber == 35063
replace competition = .2679895 if constituencynumber == 35064
replace competition = .18177 if constituencynumber == 35065
replace competition = .28704 if constituencynumber == 35066
replace competition = .28745 if constituencynumber == 35067
replace competition = .3357675 if constituencynumber == 35068
replace competition = .252946 if constituencynumber == 35069
replace competition = .342924 if constituencynumber == 35070
replace competition = .2297175 if constituencynumber == 35071
replace competition = .1689395 if constituencynumber == 35072
replace competition = .162322 if constituencynumber == 35073
replace competition = .2936835 if constituencynumber == 35074
replace competition = .252696 if constituencynumber == 35075
replace competition = .30687 if constituencynumber == 35076
replace competition = .3681141 if constituencynumber == 35077
replace competition = .393524 if constituencynumber == 35078
replace competition = .3348875 if constituencynumber == 35079
replace competition = .3362375 if constituencynumber == 35080
replace competition = .228796 if constituencynumber == 35081
replace competition = .254602 if constituencynumber == 35082
replace competition = .2665095 if constituencynumber == 35083
replace competition = .2843995 if constituencynumber == 35084
replace competition = .2403455 if constituencynumber == 35085
replace competition = .3453475 if constituencynumber == 35086
replace competition = .158996 if constituencynumber == 35087
replace competition = .15493 if constituencynumber == 35088
replace competition = .255876 if constituencynumber == 35089
replace competition = .3576375 if constituencynumber == 35090
replace competition = .3034675 if constituencynumber == 35091
replace competition = .3217355 if constituencynumber == 35092
replace competition = .2390375 if constituencynumber == 35093
replace competition = .324584 if constituencynumber == 35094
replace competition = .3439575 if constituencynumber == 35095
replace competition = .35202 if constituencynumber == 35096
replace competition = .335624 if constituencynumber == 35097
replace competition = .37395 if constituencynumber == 35098
replace competition = .250376 if constituencynumber == 35099
replace competition = .2401715 if constituencynumber == 35100
replace competition = .354214 if constituencynumber == 35101
replace competition = .233196 if constituencynumber == 35102
replace competition = .359348 if constituencynumber == 35103
replace competition = .274804 if constituencynumber == 35104
replace competition = .3504075 if constituencynumber == 35105
replace competition = .428878 if constituencynumber == 35106
replace competition = .283788 if constituencynumber == 35107
replace competition = .3568395 if constituencynumber == 35108
replace competition = .2389215 if constituencynumber == 35109
replace competition = .357946 if constituencynumber == 35110
replace competition = .145386 if constituencynumber == 35111
replace competition = .320112 if constituencynumber == 35112
replace competition = .269186 if constituencynumber == 35113
replace competition = .1873795 if constituencynumber == 35114
replace competition = .250934 if constituencynumber == 35115
replace competition = .339588 if constituencynumber == 35116
replace competition = .3678275 if constituencynumber == 35117
replace competition = .1465735 if constituencynumber == 35118
replace competition = .24878 if constituencynumber == 35119
replace competition = .34532 if constituencynumber == 35120
replace competition = .3503655 if constituencynumber == 35121
replace competition = .259912 if constituencynumber == 46001
replace competition = .222468 if constituencynumber == 46002
replace competition = .189722 if constituencynumber == 46003
replace competition = .38657 if constituencynumber == 46004
replace competition = .2530975 if constituencynumber == 46005
replace competition = .2284655 if constituencynumber == 46006
replace competition = .359686 if constituencynumber == 46007
replace competition = .274466 if constituencynumber == 46008
replace competition = .3321275 if constituencynumber == 46009
replace competition = .3362055 if constituencynumber == 46010
replace competition = .3623435 if constituencynumber == 46011
replace competition = .35147 if constituencynumber == 46012
replace competition = .261452 if constituencynumber == 46013
replace competition = .3337175 if constituencynumber == 46014
replace competition = .393794 if constituencynumber == 47001
replace competition = .3513675 if constituencynumber == 47002
replace competition = .2478035 if constituencynumber == 47003
replace competition = .364822 if constituencynumber == 47004
replace competition = .3750595 if constituencynumber == 47005
replace competition = .3442355 if constituencynumber == 47006
replace competition = .2539235 if constituencynumber == 47007
replace competition = .3275535 if constituencynumber == 47008
replace competition = .326996 if constituencynumber == 47009
replace competition = .312054 if constituencynumber == 47010
replace competition = .303084 if constituencynumber == 47011
replace competition = .2979755 if constituencynumber == 47012
replace competition = .345612 if constituencynumber == 47013
replace competition = .380406 if constituencynumber == 47014
replace competition = .3355575 if constituencynumber == 48001
replace competition = .2647175 if constituencynumber == 48002
replace competition = .2976995 if constituencynumber == 48003
replace competition = .123238 if constituencynumber == 48004
replace competition = .161124 if constituencynumber == 48005
replace competition = .292574 if constituencynumber == 48006
replace competition = .332774 if constituencynumber == 48007
replace competition = .3397835 if constituencynumber == 48008
replace competition = .3475495 if constituencynumber == 48009
replace competition = .2897275 if constituencynumber == 48010
replace competition = .32951 if constituencynumber == 48011
replace competition = .306218 if constituencynumber == 48012
replace competition = .2378095 if constituencynumber == 48013
replace competition = .273288 if constituencynumber == 48014
replace competition = .30241 if constituencynumber == 48015
replace competition = .35554 if constituencynumber == 48016
replace competition = .2164716 if constituencynumber == 48017
replace competition = .3391395 if constituencynumber == 48018
replace competition = .3336056 if constituencynumber == 48019
replace competition = .289428 if constituencynumber == 48020
replace competition = .3529515 if constituencynumber == 48021
replace competition = .3149835 if constituencynumber == 48022
replace competition = .330116 if constituencynumber == 48023
replace competition = .333134 if constituencynumber == 48024
replace competition = .3336195 if constituencynumber == 48025
replace competition = .3943935 if constituencynumber == 48026
replace competition = .3525075 if constituencynumber == 48027
replace competition = .354098 if constituencynumber == 48028
replace competition = .3235355 if constituencynumber == 48029
replace competition = .3437015 if constituencynumber == 48030
replace competition = .4313875 if constituencynumber == 48031
replace competition = .3779395 if constituencynumber == 48032
replace competition = .350228 if constituencynumber == 48033
replace competition = .3399975 if constituencynumber == 48034
replace competition = .3247735 if constituencynumber == 59001
replace competition = .3338775 if constituencynumber == 59002
replace competition = .276394 if constituencynumber == 59003
replace competition = .3227135 if constituencynumber == 59004
replace competition = .261404 if constituencynumber == 59005
replace competition = .3165975 if constituencynumber == 59006
replace competition = .2989075 if constituencynumber == 59007
replace competition = .3067115 if constituencynumber == 59008
replace competition = .3821175 if constituencynumber == 59009
replace competition = .4197295 if constituencynumber == 59010
replace competition = .3155975 if constituencynumber == 59011
replace competition = .339212 if constituencynumber == 59012
replace competition = .2923355 if constituencynumber == 59013
replace competition = .212382 if constituencynumber == 59014
replace competition = .274644 if constituencynumber == 59015
replace competition = .281754 if constituencynumber == 59016
replace competition = .3035795 if constituencynumber == 59017
replace competition = .4144175 if constituencynumber == 59018
replace competition = .3739995 if constituencynumber == 59019
replace competition = .3343 if constituencynumber == 59020
replace competition = .36692 if constituencynumber == 59021
replace competition = .2970735 if constituencynumber == 59022
replace competition = .2968675 if constituencynumber == 59023
replace competition = .398048 if constituencynumber == 59024
replace competition = .200684 if constituencynumber == 59025
replace competition = .3945 if constituencynumber == 59026
replace competition = .4221775 if constituencynumber == 59027
replace competition = .3897295 if constituencynumber == 59028
replace competition = .3225575 if constituencynumber == 59029
replace competition = .2037355 if constituencynumber == 59030
replace competition = .25128 if constituencynumber == 59031
replace competition = .351334 if constituencynumber == 59032
replace competition = .3582915 if constituencynumber == 59033
replace competition = .4102455 if constituencynumber == 59034
replace competition = .3874075 if constituencynumber == 59035
replace competition = .348664 if constituencynumber == 59036
replace competition = .378414 if constituencynumber == 59037
replace competition = .3711255 if constituencynumber == 59038
replace competition = .3629495 if constituencynumber == 59039
replace competition = .3059415 if constituencynumber == 59040
replace competition = .34728 if constituencynumber == 59041
replace competition = .383914 if constituencynumber == 59042
replace competition = .37206 if constituencynumber == 60001
replace competition = .334562 if constituencynumber == 61001
replace competition = .3455175 if constituencynumber == 62001


***************************
** INDEPENDENT VARIABLES **
***************************

* Time

gen surveydate = dofc(enddate)

format %tdMon_DD,_CCYY surveydate

gen edate = 20151019

gen electiondate=date(string(edate,"%8.0f"),"YMD")

format %tdMon_DD,_CCYY electiondate

gen time = (electiondate - surveydate) - 1

replace time = . if time < 0

* Province

gen prov = province

recode prov 1=2 2=1 3=4 4=7 5=10 7=8 9=5 10=9 11=6 12=3

label define prov 1 "British Columbia" 2 "Alberta" 3 "Saskatchewan" 4 "Manitoba" 5 "Ontario" 6 "Quebec" 7 "New Brunswick" 8 "Nova Scotia" 9 "Prince Edward Island" 10 "Newfoundland and Labrador"
label values prov prov

* Language

gen lang = .

replace lang = 0 if q_language == "EN"
replace lang = 1 if q_language == "FR"

label define language 0 "English" 1 "French"
label values language language

* Gender

gen male = gender

recode male 2=0 3=.

label define gender 0 "Female" 1 "Male"
label values male gender

* Age 

* Age 55 and over

gen age55 = 1 if age >= 55
replace age55 = 0 if age < 55 & age!=.

label define age55 0 "Less than 55" 1 "55 and over"
label values age55 age55

* Education (uniform)

gen education_unif = education
recode education_unif 1=771 2=772 3=773 4=774 5=775 6=776 7=776 8=776 9=.

label define education_unif 771 "Some high school" 772 "High school" 773 "Community college/CEGEP/Trade school" 774 "Some university" 775 "University undergraduate degree" 776 "University graduate degree"
label values education_unif education_unif

* University degree

gen univ = 1 if education >= 5
replace univ = 0 if education < 5 & education!=.

label define univ 0 "No university degree" 1 "University degree"
label values univ univ

* Postgraduate degree

gen postgrad = 1 if education >= 6
replace postgrad = 0 if education < 6 & education!=.

label define postgrad 0 "No postgraduate degree" 1 "Postgraduate degree"
label values postgrad postgrad

* Postsecondary degree

gen postsec = 1 if education >= 3
replace postsec = 0 if education < 3 & education!=.

label define postsec 0 "No postsecondary" 1 "Postsecondary"
label values postsec postsec

* Political interest

gen interest = attention_1

* High political interest

gen highint = 1 if interest > 7
replace highint = 0 if interest < 8

* Immigrant

gen immigrant = born

recode immigrant 1=0 2=1

label define immigrant 0 "Born in Canada" 1 "Immigrant"
label values immigrant immigrant

* Minority

gen minority = racial

recode minority 4=0 5/16=1 -99=.

label define minority 0 "Not visible minority" 1 "Visible minority"
label values minority minority

* Household income

recode income 8=. 9=8 10=9

* Household income (uniform)

gen income_unif = income
recode income_unif 1=771 2=772 3=773 4=774 5=775 6=776 7=776 8=776 9=776

* Household income (quintile)

xtile income5 = income, nq(5)

* Highest income categories

gen highinc = 1 if income5 == 5
replace highinc = 0 if income5 < 5 & income!=.

label define highinc 0 "Others" 1 "Highest incomes"
label values highinc highinc

* Vote choice

gen vote_choice = firstchoice
replace vote_choice = pes_vote if vote_choice == 6 | vote_choice == 7

recode vote_choice 1=2 2=1 6=88

label define vote_choice 1 "Conservative Party of Canada" 2 "Liberal Party of Canada" 3 "New Democratic Party of Canada" 4 "Bloc Quebecois" 5 "Green Party of Canada" 88 "Other party"
label values vote_choice vote_choice

* Vote status (district)

gen vote_district = 1 if vote_choice == district_outcome
replace vote_district = 0 if vote_choice!=district_outcome & vote_choice!=. 

label define winner 0 "Voted for loser" 1 "Voted for winner"
label values vote_district winner

* Vote status (national)

gen vote_whole = 1 if vote_choice == 2
replace vote_whole = 0 if vote_choice!=2 & vote_choice!=. 

label values vote_whole winner

* Party identification (district)

gen pid = id_party

recode pid 1=2 2=1

gen pidstatus_district = .

replace pidstatus_district = 3 if pid == district_outcome & pid!=.
replace pidstatus_district = 1 if pid!=district_outcome & pid!=.
replace pidstatus_district = 2 if pid == 6

replace pidstatus_district = . if pid == 7

label values pidstatus_district pidstatus

label define pidstatus 1 "Loser PID" 2 "No PID" 3 "Winner PID" 
label values pidstatus_district pidstatus

* PID strength

gen strength = id_strong

replace strength = . if strength == 4

* Party identification scale (district)

gen pidscale_district = .

replace pidscale_district = 1 if pidstatus_district == 1 & strength == 1
replace pidscale_district = 2 if pidstatus_district == 1 & strength == 2
replace pidscale_district = 3 if pidstatus_district == 1 & strength == 3
replace pidscale_district = 4 if pidstatus_district == 2
replace pidscale_district = 5 if pidstatus_district == 3 & strength == 3
replace pidscale_district = 6 if pidstatus_district == 3 & strength == 2
replace pidscale_district = 7 if pidstatus_district == 3 & strength == 1

label define pidscale 1 "Strong loser PID" 2 "Moderate loser PID" 3 "Weak loser PID" 4 "No PID" 5 "Weak winner PID" 6 "Moderate winner PID" 7 "Strong winner PID" 
label values pidscale_district pidscale

* Relative feeling toward party of choice

gen plc1 = parties_2
gen plc2 = parties_3
gen plc3 = parties_4
gen plc4 = parties_5

recode plc* (-99=.)

rowsort plc*, gen(lib1-lib4) descend

gen plc0 = parties_1
recode plc0 -99=.

gen cpc1 = parties_1
gen cpc2 = parties_3
gen cpc3 = parties_4
gen cpc4 = parties_5

recode cpc* (-99=.)

rowsort cpc*, gen(con1-con4) descend

gen cpc0 = parties_2
recode cpc0 -99=.

gen ndp1 = parties_1
gen ndp2 = parties_2
gen ndp3 = parties_4
gen ndp4 = parties_5

recode ndp* (-99=.)

rowsort ndp*, gen(new1-new4) descend

gen ndp0 = parties_3
recode ndp0 -99=.

gen bq1 = parties_1
gen bq2 = parties_2
gen bq3 = parties_3
gen bq4 = parties_5

recode bq* (-99=.)

rowsort bq*, gen(bloc1-bloc4) descend

gen bq0 = parties_4
recode bq0 -99=.

gen gpc1 = parties_1
gen gpc2 = parties_2
gen gpc3 = parties_3
gen gpc4 = parties_4

recode gpc* (-99=.)

rowsort gpc*, gen(grn1-grn4) descend

gen gpc0 = parties_5
recode gpc0 -99=.

gen first_party = .

replace first_party = plc0 if vote_choice == 2
replace first_party = cpc0 if vote_choice == 1
replace first_party = ndp0 if vote_choice == 3
replace first_party = bq0 if vote_choice == 4
replace first_party = gpc0 if vote_choice == 5

gen relative = .

replace relative = plc0 - plc1 if vote_choice == 2 
replace relative = cpc0 - cpc1 if vote_choice == 1 
replace relative = ndp0 - ndp1 if vote_choice == 3 
replace relative = bq0 - bq1 if vote_choice == 4
replace relative = gpc0 - gpc1 if vote_choice == 5


*********************************
** DISTRICT-LEVEL EXPECTATIONS **
*********************************

gen cpcchance_district = cons_pred1_2
replace cpcchance_district = cons_pred2_2 if cpcchance_district == .

gen ndpchance_district = cons_pred1_3
replace ndpchance_district = cons_pred2_3 if ndpchance_district == .  

gen lpcchance_district = cons_pred1_1
replace lpcchance_district = cons_pred2_1 if lpcchance_district == .

gen bqchance_district = cons_pred1_4

gen gpcchance_district = cons_pred1_5
replace gpcchance_district = cons_pred2_4 if gpcchance_district == .
   
* Generate variable identifying incomplete_district answers and DKs (BQ in Quebec only)

gen dk_district = 1 if cpcchance_district == -99 & ndpchance_district == -99 & lpcchance_district == -99 & bqchance_district == -99 & gpcchance_district == -99 & province == 6
replace dk_district = 1 if cpcchance_district == -99 & ndpchance_district == -99 & lpcchance_district == -99 & gpcchance_district == -99 & province!=6

recode cpcchance_district ndpchance_district lpcchance_district bqchance_district gpcchance_district (-99=.) // Recode DKs as missing values

gen complete_district_qc = 1 if cpcchance_district!=. & ndpchance_district!=. & lpcchance_district!=. & bqchance_district!=. & gpcchance_district!=. & province == 6 // If the respondent gave an expectation score to each party candidate, treat his/her answers as complete_district (Quebec respondents)

gen complete_district_roc = 1 if cpcchance_district!=. & ndpchance_district!=. & lpcchance_district!=. & gpcchance_district!=. & province!=6 // If the respondent gave an expectation score to each party candidate, treat his/her answers as complete_district (ROC respondents)

gen complete_district = max(complete_district_qc, complete_district_roc)

* Generate a value of 1 if a party candidate received the highest expectation score

gen cpc_district_qc = 1 if cpcchance_district >= ndpchance_district & cpcchance_district >= lpcchance_district & cpcchance_district >= bqchance_district & cpcchance_district >= gpcchance_district & province == 6 & complete_district == 1 // The CPC candidate received the highest expectation score given by the respondent
gen ndp_district_qc = 1 if ndpchance_district >= cpcchance_district & ndpchance_district >= lpcchance_district & ndpchance_district >= bqchance_district & ndpchance_district >= gpcchance_district & province == 6 & complete_district == 1 // The NDP candidate received the highest expectation score given by the respondent
gen lpc_district_qc = 1 if lpcchance_district >= cpcchance_district & lpcchance_district >= ndpchance_district & lpcchance_district >= bqchance_district & lpcchance_district >= gpcchance_district & province == 6 & complete_district == 1 // The LPC candidate received the highest expectation score given by the respondent
gen bq_district_qc = 1 if bqchance_district >= cpcchance_district & bqchance_district >= ndpchance_district & bqchance_district >= lpcchance_district & bqchance_district >= gpcchance_district & province == 6 & complete_district == 1 // The BQ candidate received the highest expectation score given by the respondent
gen gpc_district_qc = 1 if gpcchance_district >= cpcchance_district & gpcchance_district >= ndpchance_district & gpcchance_district >= lpcchance_district & gpcchance_district >= bqchance_district & province == 6 & complete_district == 1 // The GPC candidate received the highest expectation score given by the respondent
gen oth_district_qc = 1 if cpcchance_district == 0 & ndpchance_district == 0 & lpcchance_district == 0 & bqchance_district == 0 & gpcchance_district == 0 & province == 6 & complete_district == 1 // Another candidate received the highest expectation score given by the respondent (if a respondent gave a score of 0 to every party candidate, it is assumed that the respondent believes some other party has the highest chance of winning in the district)

gen cpc_district_roc = 1 if cpcchance_district >= ndpchance_district & cpcchance_district >= lpcchance_district & cpcchance_district >= gpcchance_district & province!=6 & complete_district == 1 // The CPC candidate received the highest expectation score given by the respondent 
gen ndp_district_roc = 1 if ndpchance_district >= cpcchance_district & ndpchance_district >= lpcchance_district & ndpchance_district >= gpcchance_district & province!=6 & complete_district == 1 // The NDP candidate received the highest expectation score given by the respondent 
gen lpc_district_roc = 1 if lpcchance_district >= cpcchance_district & lpcchance_district >= ndpchance_district & lpcchance_district >= gpcchance_district & province!=6 & complete_district == 1 // The LPC candidate received the highest expectation score given by the respondent 
gen gpc_district_roc = 1 if gpcchance_district >= cpcchance_district & gpcchance_district >= ndpchance_district & gpcchance_district >= lpcchance_district & province!=6 & complete_district == 1 // The GPC candidate received the highest expectation score given by the respondent 
gen oth_district_roc = 1 if cpcchance_district == 0 & ndpchance_district == 0 & lpcchance_district == 0 & gpcchance_district == 0 & province!=6 & complete_district == 1 // Another candidate received the highest expectation score given by the respondent (if a respondent gave a score of 0 to every party candidate, it is assumed that the respondent believes some other party has the highest chance of winning in the district)

gen cpc_district = max(cpc_district_qc, cpc_district_roc) if complete_district == 1
gen ndp_district = max(ndp_district_qc, ndp_district_roc) if complete_district == 1
gen lpc_district = max(lpc_district_qc, lpc_district_roc) if complete_district == 1
gen bq_district = bq_district_qc if complete_district == 1
gen gpc_district = max(gpc_district_qc, gpc_district_roc) if complete_district == 1
gen oth_district = max(oth_district_qc, oth_district_roc) if complete_district == 1 // Merging answers from Quebec respondents and ROC respondents

replace cpc_district = . if oth_district == 1 & complete_district == 1
replace ndp_district = . if oth_district == 1 & complete_district == 1
replace lpc_district = . if oth_district == 1 & complete_district == 1
replace bq_district = . if oth_district == 1 & complete_district == 1
replace gpc_district = . if oth_district == 1 & complete_district == 1 // This ensures respondents who gave a score of 0 to every party candidate are not treated as providing ambiguous forecasts

* Generate variable for ambiguous forecasts

gen ambiguous_district = .

replace ambiguous_district = 99 if cpc_district == ndp_district & cpc_district!=. & complete_district == 1 | cpc_district == lpc_district & cpc_district!=. & complete_district == 1 | cpc_district == bq_district & cpc_district!=. & complete_district == 1 | cpc_district == gpc_district & cpc_district!=. & complete_district == 1 // The respondent gave his/her highest expectation score to the CPC candidate and at least one other candidate
replace ambiguous_district = 99 if ndp_district == cpc_district & ndp_district!=. & complete_district == 1 | ndp_district == lpc_district & ndp_district!=. & complete_district == 1 | ndp_district == bq_district & ndp_district!=. & complete_district == 1 | ndp_district == gpc_district & ndp_district!=. & complete_district == 1 // The respondent gave his/her highest expectation score to the NDP candidate and at least one other candidate
replace ambiguous_district = 99 if lpc_district == cpc_district & lpc_district!=. & complete_district == 1 | lpc_district == ndp_district & lpc_district!=. & complete_district == 1 | lpc_district == bq_district & lpc_district!=. & complete_district == 1 | lpc_district == gpc_district & lpc_district!=. & complete_district == 1 // The respondent gave his/her highest expectation score to the LPC candidate and at least one other candidate
replace ambiguous_district = 99 if bq_district == cpc_district & bq_district!=. & complete_district == 1 | bq_district == ndp_district & bq_district!=. & complete_district == 1 | bq_district == lpc_district & bq_district!=. & complete_district == 1 | bq_district == gpc_district & bq_district!=. & complete_district == 1 // The respondent gave his/her highest expectation score to the BQ candidate and at least one other candidate
replace ambiguous_district = 99 if gpc_district == cpc_district & gpc_district!=. & complete_district == 1 | gpc_district == ndp_district & gpc_district!=. & complete_district == 1 | gpc_district == lpc_district & gpc_district!=. & complete_district == 1 | gpc_district == bq_district & gpc_district!=. & complete_district == 1 // The respondent gave his/her highest expectation score to the GPC candidate and at least one other candidate

gen amb_district = 1 if ambiguous_district == 99 & complete_district == 1

* Generate a value of 0 for lower expectations

replace cpc_district = 0 if cpc_district!=1 & complete_district == 1 // The CPC candidate did not receive the highest expectation score given by the respondent
replace ndp_district = 0 if ndp_district!=1 & complete_district == 1 // The NDP candidate did not receive the highest expectation score given by the respondent
replace lpc_district = 0 if lpc_district!=1 & complete_district == 1 // The LPC candidate did not receive the highest expectation score given by the respondent
replace bq_district = 0 if bq_district!=1 & complete_district == 1 // The BQ candidate did not receive the highest expectation score given by the respondent
replace gpc_district = 0 if gpc_district!=1 & complete_district == 1 // The GPC candidate did not receive the highest expectation score given by the respondent
replace oth_district = 0 if oth_district!=1 & complete_district == 1 // Another candidate did not receive the highest expectation score given by the respondent

* Forecasts (1 = CPC, 2 = LPC, 3 = NDP, 4 = BQ, 5 = GPC, 99 = ambiguous)

gen forecast_district = .

replace forecast_district = 1 if cpc_district == 1 & complete_district == 1 // The CPC candidate is the predicted winner
replace forecast_district = 3 if ndp_district == 1 & complete_district == 1 // The NDP candidate is the predicted winner
replace forecast_district = 2 if lpc_district == 1 & complete_district == 1 // The LPC candidate is the predicted winner
replace forecast_district = 4 if bq_district == 1 & complete_district == 1 // The BQ candidate is the predicted winner
replace forecast_district = 5 if gpc_district == 1 & complete_district == 1 // The GPC candidate is the predicted winner
replace forecast_district = 88 if oth_district == 1 & complete_district == 1 // Another candidate is the predicted winner
replace forecast_district = 99 if ambiguous_district == 99 & complete_district == 1 // Ambiguous forecast

* Identify correct and ambiguous forecasts

gen correct_district = 0 if forecast_district!=district_outcome & forecast_district!=99 & complete_district == 1 // The forecast is incorrect

replace correct_district = 1 if forecast_district == district_outcome & forecast_district!=99 & complete_district == 1 // The forecast is correct

replace correct_district = 0 if forecast_district == 99 & cpc_district!=1 & district_outcome == 1 & complete_district == 1 // The ambiguous forecast is incorrect since the CPC candidate is not among the candidates who received the highest score
replace correct_district = 0 if forecast_district == 99 & ndp_district!=1 & district_outcome == 3 & complete_district == 1 // The ambiguous forecast is incorrect since the NDP candidate is not among the candidates who received the highest score
replace correct_district = 0 if forecast_district == 99 & lpc_district!=1 & district_outcome == 2 & complete_district == 1 // The ambiguous forecast is incorrect since the LPC candidate is not among the candidates who received the highest score
replace correct_district = 0 if forecast_district == 99 & bq_district!=1 & district_outcome == 4 & complete_district == 1 // The ambiguous forecast is incorrect since the BQ candidate is not among the candidates who received the highest score
replace correct_district = 0 if forecast_district == 99 & gpc_district!=1 & district_outcome == 5 & complete_district == 1 // The ambiguous forecast is incorrect since the GPC candidate is not among the candidates who received the highest score

replace correct_district = 0 if dk_district == 1 // If the respondent answered DK for every candidate, treat as incorrect forecast

replace correct_district = 99 if forecast_district == 99 & cpc_district == 1 & district_outcome == 1 & complete_district == 1 // The forecast is truly ambiguous since the CPC candidate is among the candidates who received the highest score
replace correct_district = 99 if forecast_district == 99 & ndp_district == 1 & district_outcome == 3 & complete_district == 1 // The forecast is truly ambiguous since the NDP candidate is among the candidates who received the highest score
replace correct_district = 99 if forecast_district == 99 & lpc_district == 1 & district_outcome == 2 & complete_district == 1 // The forecast is truly ambiguous since the LPC candidate is among the candidates who received the highest score
replace correct_district = 99 if forecast_district == 99 & bq_district == 1 & district_outcome == 4 & complete_district == 1 // The forecast is truly ambiguous since the BQ candidate is among the candidates who received the highest score
replace correct_district = 99 if forecast_district == 99 & gpc_district == 1 & district_outcome == 5 & complete_district == 1 // The forecast is truly ambiguous since the GPC candidate is among the candidates who received the highest score

* Generate dichotomous variable

gen correct_district_d = correct_district

replace correct_district_d = 0 if correct_district_d == 99 // Ambiguous forecasts are treated as incorrect
replace correct_district_d = 1 if correct_district_d == 1


*********************************
** NATIONAL-LEVEL EXPECTATIONS **
*********************************

gen cpcchance_whole = seats_pred_2

gen ndpchance_whole = seats_pred_3

gen lpcchance_whole = seats_pred_1

gen bqchance_whole = seats_pred_4

gen gpcchance_whole = seats_pred_5
   
* Generate variable identifying incomplete_district answers and DKs

gen dk_whole = 1 if cpcchance_whole == -99 & ndpchance_whole == -99 & lpcchance_whole == -99 & bqchance_whole == -99 & gpcchance_whole == -99 == -99
replace dk_whole = 1 if cpcchance_whole == -99 & ndpchance_whole == -99 & lpcchance_whole == -99 & gpcchance_whole == -99

recode cpcchance_whole ndpchance_whole lpcchance_whole bqchance_whole gpcchance_whole (-99=.) // Recode DKs as missing values

gen complete_whole = 1 if cpcchance_whole!=. & ndpchance_whole!=. & lpcchance_whole!=. & bqchance_whole!=. & gpcchance_whole!=. // If the respondent gave an expectation score to each party candidate, treat his/her answers as complete

* Generate a value of 1 if a party received the highest expectation score

gen cpc_whole = 1 if cpcchance_whole >= ndpchance_whole & cpcchance_whole >= lpcchance_whole & cpcchance_whole >= bqchance_whole & cpcchance_whole >= gpcchance_whole & complete_whole == 1 // The CPC received the highest expectation score given by the respondent
gen ndp_whole = 1 if ndpchance_whole >= cpcchance_whole & ndpchance_whole >= lpcchance_whole & ndpchance_whole >= bqchance_whole & ndpchance_whole >= gpcchance_whole & complete_whole == 1 // The NDP received the highest expectation score given by the respondent
gen lpc_whole = 1 if lpcchance_whole >= cpcchance_whole & lpcchance_whole >= ndpchance_whole & lpcchance_whole >= bqchance_whole & lpcchance_whole >= gpcchance_whole & complete_whole == 1 // The LPC received the highest expectation score given by the respondent
gen bq_whole = 1 if bqchance_whole >= cpcchance_whole & bqchance_whole >= ndpchance_whole & bqchance_whole >= lpcchance_whole & bqchance_whole >= gpcchance_whole & complete_whole == 1 // The BQ received the highest expectation score given by the respondent
gen gpc_whole = 1 if gpcchance_whole >= cpcchance_whole & gpcchance_whole >= ndpchance_whole & gpcchance_whole >= lpcchance_whole & gpcchance_whole >= bqchance_whole & complete_whole == 1 // The GPC received the highest expectation score given by the respondent
gen oth_whole = 1 if cpcchance_whole == 0 & ndpchance_whole == 0 & lpcchance_whole == 0 & bqchance_whole == 0 & gpcchance_whole == 0 & complete_whole == 1 // Another received the highest expectation score given by the respondent (if a respondent gave a score of 0 to every party candidate, it is assumed that the respondent believes some other party has the highest chance of winning in the whole country)

replace cpc_whole = . if oth_whole == 1 & complete_whole == 1
replace ndp_whole = . if oth_whole == 1 & complete_whole == 1
replace lpc_whole = . if oth_whole == 1 & complete_whole == 1
replace bq_whole = . if oth_whole == 1 & complete_whole == 1
replace gpc_whole = . if oth_whole == 1 & complete_whole == 1 // This ensures respondents who gave a score of 0 to every party are not treated as providing ambiguous forecasts

* Generate variable for ambiguous forecasts

gen ambiguous_whole = .

replace ambiguous_whole = 99 if cpc_whole == ndp_whole & cpc_whole!=. & complete_whole == 1 | cpc_whole == lpc_whole & cpc_whole!=. & complete_whole == 1 | cpc_whole == bq_whole & cpc_whole!=. & complete_whole == 1 | cpc_whole == gpc_whole & cpc_whole!=. & complete_whole == 1 // The respondent gave his/her highest expectation score to the CPC and at least one other candidate
replace ambiguous_whole = 99 if ndp_whole == cpc_whole & ndp_whole!=. & complete_whole == 1 | ndp_whole == lpc_whole & ndp_whole!=. & complete_whole == 1 | ndp_whole == bq_whole & ndp_whole!=. & complete_whole == 1 | ndp_whole == gpc_whole & ndp_whole!=. & complete_whole == 1 // The respondent gave his/her highest expectation score to the NDP and at least one other candidate
replace ambiguous_whole = 99 if lpc_whole == cpc_whole & lpc_whole!=. & complete_whole == 1 | lpc_whole == ndp_whole & lpc_whole!=. & complete_whole == 1 | lpc_whole == bq_whole & lpc_whole!=. & complete_whole == 1 | lpc_whole == gpc_whole & lpc_whole!=. & complete_whole == 1 // The respondent gave his/her highest expectation score to the LPC and at least one other candidate
replace ambiguous_whole = 99 if bq_whole == cpc_whole & bq_whole!=. & complete_whole == 1 | bq_whole == ndp_whole & bq_whole!=. & complete_whole == 1 | bq_whole == lpc_whole & bq_whole!=. & complete_whole == 1 | bq_whole == gpc_whole & bq_whole!=. & complete_whole == 1 // The respondent gave his/her highest expectation score to the BQ and at least one other candidate
replace ambiguous_whole = 99 if gpc_whole == cpc_whole & gpc_whole!=. & complete_whole == 1 | gpc_whole == ndp_whole & gpc_whole!=. & complete_whole == 1 | gpc_whole == lpc_whole & gpc_whole!=. & complete_whole == 1 | gpc_whole == bq_whole & gpc_whole!=. & complete_whole == 1 // The respondent gave his/her highest expectation score to the GPC and at least one other candidate

* Generate a value of 0 for lower expectations

replace cpc_whole = 0 if cpc_whole!=1 & complete_whole == 1 // The CPC did not receive the highest expectation score given by the respondent
replace ndp_whole = 0 if ndp_whole!=1 & complete_whole == 1 // The NDP did not receive the highest expectation score given by the respondent
replace lpc_whole = 0 if lpc_whole!=1 & complete_whole == 1 // The LPC did not receive the highest expectation score given by the respondent
replace bq_whole = 0 if bq_whole!=1 & complete_whole == 1 // The BQ did not receive the highest expectation score given by the respondent
replace gpc_whole = 0 if gpc_whole!=1 & complete_whole == 1 // The GPC did not receive the highest expectation score given by the respondent
replace oth_whole = 0 if oth_whole!=1 & complete_whole == 1 // Another did not receive the highest expectation score given by the respondent

* Forecasts (1 = CPC, 2 = LPC, 3 = NDP, 4 = BQ, 5 = GPC, 99 = ambiguous)

gen forecast_whole = .

replace forecast_whole = 1 if cpc_whole == 1 & complete_whole == 1 // The CPC is the predicted winner
replace forecast_whole = 3 if ndp_whole == 1 & complete_whole == 1 // The NDP is the predicted winner
replace forecast_whole = 2 if lpc_whole == 1 & complete_whole == 1 // The LPC is the predicted winner
replace forecast_whole = 4 if bq_whole == 1 & complete_whole == 1 // The BQ is the predicted winner
replace forecast_whole = 5 if gpc_whole == 1 & complete_whole == 1 // The GPC is the predicted winner
replace forecast_whole = 88 if oth_whole == 1 & complete_whole == 1 // Another is the predicted winner
replace forecast_whole = 99 if ambiguous_whole == 99 & complete_whole == 1 // Ambiguous forecast

* Identify correct and ambiguous forecasts

gen correct_whole = 0 if forecast_whole!=2 & forecast_whole!=99 & complete_whole == 1 // The forecast is incorrect
replace correct_whole = 1 if forecast_whole == 2 & forecast_whole!=99 & complete_whole == 1 // The forecast is correct
replace correct_whole = 0 if forecast_whole == 99 & lpc_whole!=1 & complete_whole == 1 // The ambiguous forecast is incorrect since the LPC is not among the parties who received the highest score

replace correct_whole = 0 if dk_whole == 1 // If the respondent answered DK for every party, treat as incorrect forecast
replace correct_whole = 99 if forecast_whole == 99 & lpc_whole == 1 & complete_whole == 1 // The forecast is truly ambiguous since the LPC is among the parties who received the highest score

* Generate dichotomous variable

gen correct_whole_d = correct_whole

replace correct_whole_d = 0 if correct_whole_d == 99 // Ambiguous forecasts are treated as incorrect
replace correct_whole_d = 1 if correct_whole_d == 1


*************
** WEIGHTS **
*************

* Sex

gen sex_ca2015_w = male

recode sex_ca2015_w 0=1 1=2

label define sex_ca2015_w 1 "Female" 2 "Male"
label values sex_ca2015_w sex_ca2015_w

* Age

gen age_ca2015_w = age

replace age_ca2015_w = 1 if age_ca2015_w >= 18 & age_ca2015_w <= 19
replace age_ca2015_w = 2 if age_ca2015_w >= 20 & age_ca2015_w <= 24
replace age_ca2015_w = 3 if age_ca2015_w >= 25 & age_ca2015_w <= 29
replace age_ca2015_w = 4 if age_ca2015_w >= 30 & age_ca2015_w <= 34
replace age_ca2015_w = 5 if age_ca2015_w >= 35 & age_ca2015_w <= 39
replace age_ca2015_w = 6 if age_ca2015_w >= 40 & age_ca2015_w <= 44
replace age_ca2015_w = 7 if age_ca2015_w >= 45 & age_ca2015_w <= 49
replace age_ca2015_w = 8 if age_ca2015_w >= 50 & age_ca2015_w <= 54
replace age_ca2015_w = 9 if age_ca2015_w >= 55 & age_ca2015_w <= 59
replace age_ca2015_w = 10 if age_ca2015_w >= 60 & age_ca2015_w <= 64
replace age_ca2015_w = 11 if age_ca2015_w >= 65 & age_ca2015_w <= 69
replace age_ca2015_w = 12 if age_ca2015_w >= 70 & age_ca2015_w <= 74
replace age_ca2015_w = 13 if age_ca2015_w >= 75 & age_ca2015_w <= 79
replace age_ca2015_w = 14 if age_ca2015_w >= 80 & age_ca2015_w <= 84
replace age_ca2015_w = 15 if age_ca2015_w >= 85

label define age_ca2015_w 1 "18-19" 2 "20-24" 3 "25-29" 4 "30-34" 5 "35-39" 6 "40-44" 7 "45-49" 8 "50-54" 9 "55-59" 10 "60-64" 11 "65-69" 12 "70-74" 13 "75-79" 14 "80-84" 15 "85+"
label values age_ca2015_w age_ca2015_w

* Education

gen education_ca2015_w = education

recode education_ca2015_w 4=3 5=4 6=5 7=5 8=5 9=.

label define education_ca2015_w 1 "Below high school" 2 "High school" 3 "Postsecondary below bachelor" 4 "University undergraduate degree" 5 "University graduate degree"
label values education_ca2015_w education_ca2015_w

* Income

gen income_ca2015_w = income

recode income_ca2015_w 7=6 8=7 9=8

label define income_ca2015_w 1 "Less than $20,000" 2 "$20,000-$39,999" 3 "$40,000-$59,999" 4 "$60,000-$79,999" 5 "$80,000-$99,999" 6 "$100,000-$149,999" 7 "$150,000-$199,999" 8 "$200,000 or more"
label values income_ca2015_w income_ca2015_w

* Province

gen province_ca2015_w = prov

replace province_ca2015_w = 9 if prov == 1
replace province_ca2015_w = 10 if prov == 2
replace province_ca2015_w = 7 if prov == 3
replace province_ca2015_w = 4 if prov == 4
replace province_ca2015_w = 1 if prov == 5
replace province_ca2015_w = 3 if prov == 6
replace province_ca2015_w = 6 if prov == 7
replace province_ca2015_w = 2 if prov == 8
replace province_ca2015_w = 5 if prov == 9
replace province_ca2015_w = 8 if prov == 10

label define province_ca2015_w 1 "Newfoundland and Labrador" 2 "Prince Edward Island" 3 "Nova Scotia" 4 "New Brunswick" 5 "Quebec" 6 "Ontario" 7 "Manitoba" 8 "Saskatchewan" 9 "Alberta" 10 "British Columbia"
label values province_ca2015_w province_ca2015_w


****************************
** PLURALITY-RANGE VOTING **
****************************

* Generate weighting variables

egen min_ability = min(interest)
egen max_ability = max(interest)

gen ability_w = (interest - min_ability)/(max_ability - min_ability)

* Generate percentage of correct forecasts for each party by district (mean will be 0 for BQ outside Quebec)

gen percent_cpc = 1 if forecast_district == 1 & ability_w!=.
replace percent_cpc = 0 if percent_cpc!=1 & ability_w!=.

gen percent_ndp = 1 if forecast_district == 3 & ability_w!=.
replace percent_ndp = 0 if percent_ndp!=1 & ability_w!=.

gen percent_lpc = 1 if forecast_district == 2 & ability_w!=.
replace percent_lpc = 0 if percent_lpc!=1 & ability_w!=.

gen percent_bq = 1 if forecast_district == 4 & ability_w!=.
replace percent_bq = 0 if percent_bq!=1 & ability_w!=.

gen percent_gpc = 1 if forecast_district == 5 & ability_w!=.
replace percent_gpc = 0 if percent_gpc!=1 & ability_w!=.

gen percent_oth = 1 if forecast_district == 88 & ability_w!=.
replace percent_oth = 0 if percent_oth!=1 & ability_w!=.

gen percent_amb = 1 if forecast_district == 99 & ability_w!=.
replace percent_amb = 0 if percent_amb!=1 & ability_w!=.

egen mean_cpc = mean(percent_cpc), by(constituencynumber)
egen mean_ndp = mean(percent_ndp), by(constituencynumber)
egen mean_lpc = mean(percent_lpc), by(constituencynumber)
egen mean_bq = mean(percent_bq), by(constituencynumber)
egen mean_gpc = mean(percent_gpc), by(constituencynumber)
egen mean_oth = mean(percent_oth), by(constituencynumber)
egen mean_amb = mean(percent_amb), by(constituencynumber)

* Generate weighted forecast for each respondent

gen cpc_w1 = cpc_district*ability_w
gen ndp_w1 = ndp_district*ability_w
gen lpc_w1 = lpc_district*ability_w
gen bq_w1 = bq_district*ability_w
gen gpc_w1 = gpc_district*ability_w
gen oth_w1 = oth_district*ability_w
gen amb_w1 = amb_district*ability_w

* Generate sum of expectations for each party by district

egen cpc_sum_w1 = sum(cpc_w1) if complete_district == 1 & ability_w!=., by(constituencynumber)
egen ndp_sum_w1 = sum(ndp_w1) if complete_district == 1 & ability_w!=., by(constituencynumber)
egen lpc_sum_w1 = sum(lpc_w1) if complete_district == 1 & ability_w!=., by(constituencynumber)
egen bq_sum_w1 = sum(bq_w1) if complete_district == 1 & ability_w!=., by(constituencynumber)
egen gpc_sum_w1 = sum(gpc_w1) if complete_district == 1 & ability_w!=., by(constituencynumber)
egen oth_sum_w1 = sum(oth_w1) if complete_district == 1 & ability_w!=., by(constituencynumber)
egen amb_sum_w1 = sum(amb_w1) if complete_district == 1 & ability_w!=., by(constituencynumber)

* Generate value of 1 if the sum of expectations for one candidate in a district is higher or equal to the sum of expectations for other candidates

gen r_cpc_w1 = 1 if cpc_sum_w1 >= ndp_sum_w1 & cpc_sum_w1 >= lpc_sum_w1 & cpc_sum_w1 >= bq_sum_w1 & cpc_sum_w1 >= gpc_sum_w1 & cpc_sum_w1 >= oth_sum_w1 & cpc_sum_w1 >= amb_sum_w1
gen r_ndp_w1 = 1 if ndp_sum_w1 >= cpc_sum_w1 & ndp_sum_w1 >= lpc_sum_w1 & ndp_sum_w1 >= bq_sum_w1 & ndp_sum_w1 >= gpc_sum_w1 & ndp_sum_w1 >= oth_sum_w1 & ndp_sum_w1 >= amb_sum_w1
gen r_lpc_w1 = 1 if lpc_sum_w1 >= cpc_sum_w1 & lpc_sum_w1 >= ndp_sum_w1 & lpc_sum_w1 >= bq_sum_w1 & lpc_sum_w1 >= gpc_sum_w1 & lpc_sum_w1 >= oth_sum_w1 & lpc_sum_w1 >= amb_sum_w1
gen r_bq_w1 = 1 if bq_sum_w1 >= cpc_sum_w1 & bq_sum_w1 >= ndp_sum_w1 & bq_sum_w1 >= lpc_sum_w1 & bq_sum_w1 >= gpc_sum_w1 & bq_sum_w1 >= oth_sum_w1 & bq_sum_w1 >= amb_sum_w1
gen r_gpc_w1 = 1 if gpc_sum_w1 >= cpc_sum_w1 & gpc_sum_w1 >= ndp_sum_w1 & gpc_sum_w1 >= lpc_sum_w1 & gpc_sum_w1 >= bq_sum_w1 & gpc_sum_w1 >= oth_sum_w1 & gpc_sum_w1 >= amb_sum_w1
gen r_oth_w1 = 1 if oth_sum_w1 >= cpc_sum_w1 & oth_sum_w1 >= ndp_sum_w1 & oth_sum_w1 >= lpc_sum_w1 & oth_sum_w1 >= bq_sum_w1 & oth_sum_w1 >= gpc_sum_w1 & oth_sum_w1 >= amb_sum_w1
gen r_amb_w1 = 1 if amb_sum_w1 >= cpc_sum_w1 & amb_sum_w1 >= ndp_sum_w1 & amb_sum_w1 >= lpc_sum_w1 & amb_sum_w1 >= bq_sum_w1 & amb_sum_w1 >= gpc_sum_w1 & amb_sum_w1 >= oth_sum_w1

* Generate variable for districts with equal sums of expectations for two or more candidates

gen sum_tie_w1 = .

replace sum_tie_w1 = 999 if r_cpc_w1 == r_ndp_w1 & r_cpc_w1!=. | r_cpc_w1 == r_lpc_w1 & r_cpc_w1!=. | r_cpc_w1 == r_bq_w1 & r_cpc_w1!=. | r_cpc_w1 == r_gpc_w1 & r_cpc_w1!=. | r_cpc_w1 == r_oth_w1 & r_cpc_w1!=. | r_cpc_w1 == r_amb_w1 & r_cpc_w1!=.
replace sum_tie_w1 = 999 if r_ndp_w1 == r_cpc_w1 & r_ndp_w1!=. | r_ndp_w1 == r_lpc_w1 & r_ndp_w1!=. | r_ndp_w1 == r_bq_w1 & r_ndp_w1!=. | r_ndp_w1 == r_gpc_w1 & r_ndp_w1!=. | r_ndp_w1 == r_oth_w1 & r_ndp_w1!=. | r_ndp_w1 == r_amb_w1 & r_ndp_w1!=.
replace sum_tie_w1 = 999 if r_lpc_w1 == r_cpc_w1 & r_lpc_w1!=. | r_lpc_w1 == r_ndp_w1 & r_lpc_w1!=. | r_lpc_w1 == r_bq_w1 & r_lpc_w1!=. | r_lpc_w1 == r_gpc_w1 & r_lpc_w1!=. | r_lpc_w1 == r_oth_w1 & r_lpc_w1!=. | r_lpc_w1 == r_amb_w1 & r_lpc_w1!=.
replace sum_tie_w1 = 999 if r_bq_w1 == r_cpc_w1 & r_bq_w1!=. | r_bq_w1 == r_ndp_w1 & r_bq_w1!=. | r_bq_w1 == r_lpc_w1 & r_bq_w1!=. | r_bq_w1 == r_gpc_w1 & r_bq_w1!=. | r_bq_w1 == r_oth_w1 & r_bq_w1!=. | r_bq_w1 == r_amb_w1 & r_bq_w1!=.
replace sum_tie_w1 = 999 if r_gpc_w1 == r_cpc_w1 & r_gpc_w1!=. | r_gpc_w1 == r_ndp_w1 & r_gpc_w1!=. | r_gpc_w1 == r_lpc_w1 & r_gpc_w1!=. | r_gpc_w1 == r_bq_w1 & r_gpc_w1!=. | r_gpc_w1 == r_oth_w1 & r_gpc_w1!=. | r_gpc_w1 == r_amb_w1 & r_gpc_w1!=.
replace sum_tie_w1 = 999 if r_oth_w1 == r_cpc_w1 & r_oth_w1!=. | r_oth_w1 == r_ndp_w1 & r_oth_w1!=. | r_oth_w1 == r_lpc_w1 & r_oth_w1!=. | r_oth_w1 == r_bq_w1 & r_oth_w1!=. | r_oth_w1 == r_gpc_w1 & r_oth_w1!=. | r_oth_w1 == r_amb_w1 & r_oth_w1!=.
replace sum_tie_w1 = 999 if r_amb_w1 == r_cpc_w1 & r_amb_w1!=. | r_amb_w1 == r_ndp_w1 & r_amb_w1!=. | r_amb_w1 == r_lpc_w1 & r_amb_w1!=. | r_amb_w1 == r_bq_w1 & r_amb_w1!=. | r_amb_w1 == r_gpc_w1 & r_amb_w1!=. | r_amb_w1 == r_oth_w1 & r_amb_w1!=.

* Generate value of 0 for lower sums of expectations

replace r_cpc_w1 = 0 if r_cpc_w1!=1
replace r_ndp_w1 = 0 if r_ndp_w1!=1
replace r_lpc_w1 = 0 if r_lpc_w1!=1
replace r_bq_w1 = 0 if r_bq_w1!=1
replace r_gpc_w1 = 0 if r_gpc_w1!=1
replace r_oth_w1 = 0 if r_oth_w1!=1
replace r_amb_w1 = 0 if r_amb_w1!=1

* Identify party with highest sums of expectations in each district (1 = CPC, 2 = NDP, 3 = LPC, 4 = BQ, 5 = GPC, 88 = other, 99 = ambiguous, 999 = tie)

gen r_w1 = .

replace r_w1 = 1 if r_cpc_w1 == 1
replace r_w1 = 3 if r_ndp_w1 == 1
replace r_w1 = 2 if r_lpc_w1 == 1
replace r_w1 = 4 if r_bq_w1 == 1
replace r_w1 = 5 if r_gpc_w1 == 1
replace r_w1 = 88 if r_oth_w1 == 1
replace r_w1 = 99 if r_amb_w1 == 1
replace r_w1 = 999 if sum_tie_w1 == 999


**********
** SAVE **
**********

save "ca2015_lpp.dta", replace

* Drop district duplicates

drop if ability_w == .

duplicates tag constituencynumber, gen(dupCA15)
duplicates drop constituencynumber, force

* Generate number of respondents by district

gen numberCA15 = dupCA15 + 1

* Identify party with highest number of highest expectations for each district (1 = CPC, 2 = LPC, 3 = NDP, 4 = BQ, 5 = GPC, 88 = other, 99 = ambiguous)

gen p = .

replace p = 1 if mean_cpc > mean_ndp & mean_cpc > mean_lpc & mean_cpc > mean_bq & mean_cpc > mean_gpc & mean_cpc > mean_oth & mean_cpc > mean_amb // A plurality of respondents in the district forecast a victory of the CPC candidate
replace p = 3 if mean_ndp > mean_cpc & mean_ndp > mean_lpc & mean_ndp > mean_bq & mean_ndp > mean_gpc & mean_ndp > mean_oth & mean_ndp > mean_amb // A plurality of respondents in the district forecast a victory of the NDP candidate
replace p = 2 if mean_lpc > mean_cpc & mean_lpc > mean_ndp & mean_lpc > mean_bq & mean_lpc > mean_gpc & mean_lpc > mean_oth & mean_lpc > mean_amb // A plurality of respondents in the district forecast a victory of the LPC candidate
replace p = 4 if mean_bq > mean_cpc & mean_bq > mean_ndp & mean_bq > mean_lpc & mean_bq > mean_gpc & mean_bq > mean_oth & mean_bq > mean_amb // A plurality of respondents in the district forecast a victory of the BQ candidate
replace p = 5 if mean_gpc > mean_cpc & mean_gpc > mean_ndp & mean_gpc > mean_lpc & mean_gpc> mean_bq & mean_gpc > mean_oth & mean_gpc > mean_amb // A plurality of respondents in the district forecast a victory of the GPC candidate
replace p = 88 if mean_oth > mean_cpc & mean_oth > mean_ndp & mean_oth > mean_lpc & mean_oth > mean_bq & mean_oth > mean_gpc & mean_oth > mean_amb // A plurality of respondents in the district forecast a victory of another candidate
replace p = 99 if mean_amb > mean_cpc & mean_amb > mean_ndp & mean_amb > mean_lpc & mean_amb > mean_bq & mean_amb > mean_gpc & mean_amb > mean_oth // A plurality of respondents in the district made ambiguous forecasts
replace p = 999 if p == . // There is a tie between two or more candidates in the forecasts of respondents

* Identify correct and ambiguous/tied plurality forecasts at the district level (unweigthed)

gen p_correct = 0 if p!=district_outcome & p!=99 & p!=999
replace p_correct = 1 if p == district_outcome & p!=99 & p!=999
replace p_correct = 99 if p_correct == .

gen p_correct_d = p_correct
replace p_correct_d = 0 if p_correct_d == 99 // Treat ambiguous/tied plurality forecasts as incorrect

* Identify correct plurality-range forecasts at the district level

gen r_correct_w1 = 0 if r_w1!=district_outcome & r_w1!=999
replace r_correct_w1 = 1 if r_w1 == district_outcome & r_w1!=999
replace r_correct_w1 = 99 if r_w1 == 999

gen r_correct_w1_d = r_correct_w1
replace r_correct_w1_d = 0 if r_correct_w1_d == 99 // Treat tied range forecasts as incorrect

log close